我该怎么用? [英] What should I Use ?
问题描述
大家好,
想象一下我有一个int数组:
int [] anArray = new int [.. ];
我想提取所有优于500的整数
我能做到:
var result = new List< int>();
foreach(int in aArray)
if(i 500)
result.Add(i);
或
var resultFind = Array.FindAll(anArray,elt = elt 500);
或
var resultWhere = anArray.Where(elt = elt 500);
或
var resultLinq =来自i in aArray,其中我500选择i;
最好的方法是什么?
感谢您的回答
Hi all,
Imagine I''ve an array of int :
int[] anArray = new int[..];
I want to extract all the integer that are superior to 500
I can do :
var result = new List<int>();
foreach (int i in anArray)
if (i 500)
result.Add(i);
or
var resultFind = Array.FindAll(anArray, elt =elt 500);
or
var resultWhere = anArray.Where(elt =elt 500);
or
var resultLinq = from i in anArray where i 500 select i;
What is the best way ?
Thanks for your answer
推荐答案
ti ********* @ gmail.com 写道:
想象一下我有一个int数组:
int [] anArray = new int [..];
我想要提取所有优于500的整数
我能做到:
var result = new List< int>();
foreach(int in anArray)
if(i 500)
result.Add(i);
或
var resultFind = Array.FindAll(anArray,elt = elt 500);
或
var resultWhere = anArray.Where(elt = elt 500);
或
var resultLinq = from i在anArray中我选择i;
最好的方法是什么?
Imagine I''ve an array of int :
int[] anArray = new int[..];
I want to extract all the integer that are superior to 500
I can do :
var result = new List<int>();
foreach (int i in anArray)
if (i 500)
result.Add(i);
or
var resultFind = Array.FindAll(anArray, elt =elt 500);
or
var resultWhere = anArray.Where(elt =elt 500);
or
var resultLinq = from i in anArray where i 500 select i;
What is the best way ?
方法#1和#2创建新的收藏品,#3和#4没有(事实上他们确实没有创造任何东西;只有在评估时才会产生新的序列。
方法#3和#4使用LINQ(因此需要.NET 3.5),方法#1和#2做
$ b $不是。可读性是主观的,但#1明显不如
其他三种方法。
因此答案是可预测的它取决于。一般来说,我会选择
在上下文中哪一方最方便(我需要一个List,一个
数组,一个IEnumerable吗?)并在转换时进行分析结果或其他
情况另有规定。孤立地说,这是一个太小的问题,
选择最佳方式。
-
J.
Methods #1 and #2 create new collections, #3 and #4 do not (in fact they do
not create anything; they will yield a new sequence only when evaluated).
Methods #3 and #4 use LINQ (and thus require .NET 3.5), methods #1 and #2 do
not. Readability is subjective, but #1 is obviously less direct than the
other three methods.
The answer is therefore predictably "it depends". In general, I''d pick
whichever one was most convenient in the context (do I need a List, an
Array, an IEnumerable?) and switch if profiling results or other
circumstances dictated otherwise. in isolation, it''s too small a problem to
pick the "best way".
--
J.
我的建议是写一个小的Windows窗体应用程序,它会产生一个大的数字,就像你要搜索的那个数字一样,然后在每一个后面添加4个按钮和
,把代码放到其中一个方法中。花时间执行
使用秒表进行搜索并查看每次拍摄需要多长时间。
秒表应该是一个高精度计时器,用于执行类似
的操作
这些。
尝试使阵列变大,因此技术之间的差异是暴露的。
发布结果,让我们知道。
-
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
" ti ****** ***@gmail.com"写道:
My suggestion would be to write a small windows forms app that makes a LARGE
array of number like the one you have to search, then add 4 buttons and
behind each one, put the code to do one of the methods. Time the execution of
the search using a stopwatch and see how long each takes.
The stopwatch is supposed to be a high accuracy timer for doing things like
these.
Try to make the array large so the difference between the techniques are
exposed.
Post the results and let us know.
--
Ciaran O''''Donnell
http://wannabedeveloper.spaces.live.com
"ti*********@gmail.com" wrote:
大家好,
想象一下我有一个int数组:
>
int [] anArray = new int [..];
我想提取所有优于500的整数
我能做到:
var result = new List< int>();
foreach(int in aArray)
if(i 500)
result.Add(i);
或
var resultFind = Array.FindAll(anArray,elt = elt 500);
或
var resultWhere = anArray.Where(elt = elt 500) ;
或
var resultLinq =来自i in aArray,其中我500选择i;
最好的方法是什么?
感谢您的回答
Hi all,
Imagine I''ve an array of int :
int[] anArray = new int[..];
I want to extract all the integer that are superior to 500
I can do :
var result = new List<int>();
foreach (int i in anArray)
if (i 500)
result.Add(i);
or
var resultFind = Array.FindAll(anArray, elt =elt 500);
or
var resultWhere = anArray.Where(elt =elt 500);
or
var resultLinq = from i in anArray where i 500 select i;
What is the best way ?
Thanks for your answer
Ciaran O''''Donnell< ; Ci ************ @ discussion.microsoft.comwrote:
Ciaran O''''Donnell <Ci************@discussions.microsoft.comwrote:
我的建议是w一个小的Windows窗体应用程序,使得你需要搜索一个大数字的数组,然后添加4个按钮和
后面的每一个,把代码放到做其中一种方法。花时间执行
使用秒表进行搜索并查看每次拍摄需要多长时间。
秒表应该是一个高精度计时器,用于执行类似
的操作
这些。
尝试使阵列变大,因此技术之间的差异是暴露的。
发布结果,让我们知道。
My suggestion would be to write a small windows forms app that makes a LARGE
array of number like the one you have to search, then add 4 buttons and
behind each one, put the code to do one of the methods. Time the execution of
the search using a stopwatch and see how long each takes.
The stopwatch is supposed to be a high accuracy timer for doing things like
these.
Try to make the array large so the difference between the techniques are
exposed.
Post the results and let us know.
请注意,这表示最佳 ==最快。我个人会选择
最可读的方法。对我来说,那是:
anArrayWhere(elt = elt 500);
我只会开始担心性能我有一些证据表明
非常重要。
-
Jon Skeet - < sk *** @ pobox.com>
网站: http:// www。 pobox.com/~skeet
博客: http: //www.msmvps.com/jon_skeet
C#深度: http ://csharpindepth.com
Note that that suggests that "best" == "fastest". Personally I''d pick
the means which is the most readable. For me, that''s:
anArrayWhere(elt =elt 500);
I''d only start worrying about performance when I had some evidence that
it was really significant.
--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
这篇关于我该怎么用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!