由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
DotNet版 - Which one is faster
相关主题
c#有没有第三方地免费解释器?请问arraylist的问题。
How to compile multiple file using vbc?请教:是否可以overwrite session variable里面的item.
麻烦大家帮忙看看:cannot find dependencyNdoc - .Net's javadoc
error of compiling C# in visual studion 2013 win 7 (转载)MSDN 上的PATTERN 资源
请问C#中如何update hashtable中的value?[转载] 最近要用VC#.net编成,请教一下
问个基础问题请推荐asp.net的书!
LinkedList有用吗?MSDN 上的相关新闻组
请问.Net相关的工作,面试是什么风格的?How to write Virtual Channel Application
相关话题的讨论汇总
话题: faster话题: case话题: list话题: string话题: elements
进入DotNet版参与讨论
1 (共1页)
c**t
发帖数: 2744
1
Given List targets (around 20 elements), how check if string s is in
the list, we have the following ways:
case 1: targets.Contains(s);
case 2: if-else...
case 3: switch ...
case 4: s == e1 || s == e2 || ...
which one is faster?
a9
发帖数: 21638
2
你这是找几个啊?呵呵。

in

【在 c**t 的大作中提到】
: Given List targets (around 20 elements), how check if string s is in
: the list, we have the following ways:
: case 1: targets.Contains(s);
: case 2: if-else...
: case 3: switch ...
: case 4: s == e1 || s == e2 || ...
: which one is faster?

s***o
发帖数: 2191
3
my guess:
switch-case is the fastest since most compilers optimize it.
Contains is the slowest due to some overhead.
Conclusion: who cares...

is in

【在 c**t 的大作中提到】
: Given List targets (around 20 elements), how check if string s is in
: the list, we have the following ways:
: case 1: targets.Contains(s);
: case 2: if-else...
: case 3: switch ...
: case 4: s == e1 || s == e2 || ...
: which one is faster?

a9
发帖数: 21638
4
most compilers....
难道.net不就一个编译器吗?呵呵

【在 s***o 的大作中提到】
: my guess:
: switch-case is the fastest since most compilers optimize it.
: Contains is the slowest due to some overhead.
: Conclusion: who cares...
:
: is in

c**d
发帖数: 579
5
switch怎么找法?
r****y
发帖数: 26819
6
20个元素?这点区别应该很小吧

in

【在 c**t 的大作中提到】
: Given List targets (around 20 elements), how check if string s is in
: the list, we have the following ways:
: case 1: targets.Contains(s);
: case 2: if-else...
: case 3: switch ...
: case 4: s == e1 || s == e2 || ...
: which one is faster?

a9
发帖数: 21638
7
貌似有个阀值,如果大于那个阀值,就会创建一个dictonary,在dictonary里查找 。

【在 c**d 的大作中提到】
: switch怎么找法?
c**t
发帖数: 2744
8
randomly generated 20 elements and a new value to lookup, 10000 iteration:
case 2 < case 3 < case 4 < case 1. If-else runs fastest.

Given List targets (around 20 elements), how check if string s is in
the list, we have the following ways:
case 1: targets.Contains(s);
case 2: if-else...
case 3: switch ...
case 4: s == e1 || s == e2 || ...
which one is faster?

【在 c**t 的大作中提到】
: Given List targets (around 20 elements), how check if string s is in
: the list, we have the following ways:
: case 1: targets.Contains(s);
: case 2: if-else...
: case 3: switch ...
: case 4: s == e1 || s == e2 || ...
: which one is faster?

o****e
发帖数: 916
9
dont waste your time, just use list.Contains
generic List is smart, it builds a hashtable of the collection is big. I've
tested with 1k elements, List.Contains has no performance diff compared to
Dictionary.ContainKey
v******n
发帖数: 421
10
I think it's using linear search. You can step into .net fx source code
to see the implementation
http://www.hanselman.com/blog/NETFrameworkLibrarySourceCodeAvai

ve

【在 o****e 的大作中提到】
: dont waste your time, just use list.Contains
: generic List is smart, it builds a hashtable of the collection is big. I've
: tested with 1k elements, List.Contains has no performance diff compared to
: Dictionary.ContainKey

相关主题
问个基础问题请问arraylist的问题。
LinkedList有用吗?请教:是否可以overwrite session variable里面的item.
请问.Net相关的工作,面试是什么风格的?Ndoc - .Net's javadoc
进入DotNet版参与讨论
c**t
发帖数: 2744
11
I like list.contains more than other ways, even it's performing nano seconds
longer. The code is much cleaner, and easier to maintain and to scale.
s***o
发帖数: 2191
12
scalability vs efficiency, there is always something to weigh and make
one's head spin

seconds

【在 c**t 的大作中提到】
: I like list.contains more than other ways, even it's performing nano seconds
: longer. The code is much cleaner, and easier to maintain and to scale.

a9
发帖数: 21638
13
List也会创建hashtable?

ve

【在 o****e 的大作中提到】
: dont waste your time, just use list.Contains
: generic List is smart, it builds a hashtable of the collection is big. I've
: tested with 1k elements, List.Contains has no performance diff compared to
: Dictionary.ContainKey

s***o
发帖数: 2191
14
from MSDN
"This method performs a linear search; therefore, this method is an O(n)
operation, where n is Count."

【在 a9 的大作中提到】
: List也会创建hashtable?
:
: ve

a9
发帖数: 21638
15
也就是说不会创建喽?呵呵。

【在 s***o 的大作中提到】
: from MSDN
: "This method performs a linear search; therefore, this method is an O(n)
: operation, where n is Count."

o****e
发帖数: 916
16
take my word back, list with several k is not enough to tell the difference.
the List.Contains call is apparently O(n), just tested with 1M and 10M list
. well, even for such big list, the difference is only 20ms vs 200ms.
R********0
发帖数: 133
17
20 element it does not matter, it should run less then 1 ms. I would use
contain it is easy to read.
R********0
发帖数: 133
18
you can use
system.diagnostics.stopwatch to time it.
1 (共1页)
进入DotNet版参与讨论
相关主题
How to write Virtual Channel Application请问C#中如何update hashtable中的value?
谁有PDC WHIDBEY DOWNLOAD?问个基础问题
怎么publish ASP.NET webLinkedList有用吗?
Whidbey请问.Net相关的工作,面试是什么风格的?
c#有没有第三方地免费解释器?请问arraylist的问题。
How to compile multiple file using vbc?请教:是否可以overwrite session variable里面的item.
麻烦大家帮忙看看:cannot find dependencyNdoc - .Net's javadoc
error of compiling C# in visual studion 2013 win 7 (转载)MSDN 上的PATTERN 资源
相关话题的讨论汇总
话题: faster话题: case话题: list话题: string话题: elements