avatar
c*t
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?
avatar
a9
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?

avatar
s*o
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?

avatar
a9
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

avatar
c*d
5
switch怎么找法?
avatar
r*y
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?

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

【在 c**d 的大作中提到】
: switch怎么找法?
avatar
c*t
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?

avatar
o*e
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
avatar
v*n
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

avatar
c*t
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.
avatar
s*o
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.

avatar
a9
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

avatar
s*o
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

avatar
a9
15
也就是说不会创建喽?呵呵。

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

avatar
o*e
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.
avatar
R*0
17
20 element it does not matter, it should run less then 1 ms. I would use
contain it is easy to read.
avatar
R*0
18
you can use
system.diagnostics.stopwatch to time it.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。