- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我的问题是根据this question提出的,我已经发布了关于那个问题的答案.. here
这是代码。
var lines = System.IO.File.ReadLines(@"C:\test.txt");
var Minimum = lines[0];//Default length set
var Maximum = "";
foreach (string line in lines)
{
if (Maximum.Length < line.Length)
{
Maximum = line;
}
if (Minimum.Length > line.Length)
{
Minimum = line;
}
}
以及使用 LINQ 替代此代码(我的方法)
var lines = System.IO.File.ReadLines(@"C:\test.txt");
var Maximum = lines.OrderByDescending(a => a.Length).First().ToString();
var Minimum = lines.OrderBy(a => a.Length).First().ToString();
LINQ 易于阅读和实现..
我想知道哪个对性能有好处。Linq 如何在内部为 OrderByDescending 和 OrderBy 按长度排序?
最佳答案
您可以 read the source code for OrderBy .
停止做 micro-optimizing或 premature-optimization在你的代码上。尝试编写正确执行的代码,然后如果您稍后遇到性能问题,则分析您的应用程序并查看问题出在哪里。如果您有一段代码由于寻找最短和最长的字符串而出现性能问题,那么请开始优化这部分。
We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3% - Donald Knuth
File.ReadLines
正在返回 IEnumerable<string>
,这意味着如果你对它做一个foreach,它会一个一个地返回数据给你。我认为您在这里可以做的最好的性能改进是改进从磁盘读取文件。如果它足够小,可以将整个文件加载到内存中,请使用 File.ReadAllLines
,如果不是,请尝试以适合内存的大块读取文件。逐行读取文件会因磁盘 I/O 操作而导致性能下降。所以这里的问题不是 LINQ 或循环如何执行,而是磁盘读取次数的问题。
关于c# - 字符串长度的 linq 函数 OrderByDescending 和 OrderBy 在内部如何工作?它比用循环做更快吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043496/
所以我尝试使用angularjs的orderby函数。目前我有一个原始数据集。 $scope.customers = [ {"name" : "Bottom-Dollar Market
我有一个包含以下数据的表列 (Varchar): COLUMN_NAME 102100 1-2000 112100 当我在 SQL 语句中使用 OrderBy 时,我按以下顺序获取项目: COLUMN
我正在研究一些 LINQ 排序,因为我有一个 ID 列表,我需要按顺序对它们进行排序。但是,某些 ID 需要优先于标准顺序。 鉴于此 C# 代码(可以粘贴到 .NET Fiddle 中进行测试),排序
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 9 年前。 Improve t
我正在寻找这些 LINQ 表达式的确认/澄清: var context = new SomeCustomDbContext() // LINQ to Entities? var items = co
我的数据在我的 firebase 中看起来像这样。 users: { -KOwKNCB5IiDBxY9FNzR: { content: { blah blah }, timestamp:147
在尝试找出一段代码不同步的原因后,我开始意识到 TSQL OrderBy 与 Linq OrderBy 对于字符串的差异很大。所以很自然地,我必须找到一种方法来确保来自 tsql 的语句返回与 lin
目前,如果我们将订购方向作为外部依赖项,我们必须使用 if 来应用此方向: public static IEnumerable getlist(string directory, string sea
我在我的范围内有一个对象列表,我想遍历它们,以按某些属性排序的方式显示它们的一些属性并更改它们。 ng-repeat 用于显示绑定(bind)到列表中每个对象的文本框,并应用以“position”作为
我在 C# 项目中使用 EntityFramework 6.1.3 和 SQL Server。我有两个查询,基本上应该执行相同的操作。 1. Exams.GroupBy(x=>x.SubjectID)
我正在尝试以 asc 顺序显示从今天开始的特定接下来几天的约会列表。(例如:接下来的 5 天或 10 天)。仅从今天开始,接下来的 5 天或 10 天(注意:天数将是动态的,我的意思是客户将设置)。当
这个问题已经有答案了: 已关闭12 年前。 Possible Duplicates: Which LINQ syntax do you prefer? Fluent or Query Expressi
我目前有一个 OData V4 服务,它具有以下模型。 “类别”——“代码” 对于每个类别,可以有许多代码。 我需要 $expand the Codes, $filter where Active =
可以说注释表具有以下结构: id | author | timestamp | body 我想使用索引有效地执行以下查询: r.table('comments').getAll("me", {inde
是否可以像这样使用 LINQ OrderBy: .OrderBy(x=>(x.SourceID == 3), (x.SourceID == 2), (x=>x.SourceID == 4), (x.S
我有一个这样的数组: $scope.telcodes = ['44', '01', '221', '335']; 如何在像这样的简单数组上使用 orderBy 来生成从 01 开始的有序列表? 我知道
我有一个像这样 Eloquent 查询: Forum::with(['comments.user'])->find($id); 这将返回嵌套结果 forum -> its comments -> us
我正在尝试通过 ManyToMany 注释自动对报告的结果进行排序 @OrderBy : /** * @ORM\ManyToMany(targetEntity="Artist", inversedB
是否有按几列对 data.frame 进行排序的标准方法,但随着减少或增加的变化?例如,您可能希望按一个变量(递减)和下一个(递增)对 data.frame 进行排序。 有没有类似的东西: mydf[
我有一个 linq 查询,无论出于何种原因,它都没有像我期望的那样返回订购。任何人都可以指出我为什么以及我做错了什么的正确方向吗? 代码如下: List designer = null; using
我是一名优秀的程序员,十分优秀!