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 | |
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
|
|
|
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. |