简体   繁体   中英

Which is faster? Set comparison or Counter comparison? why

I have two strings:

a = "hello world"
b = "world hello"

which would run faster?

set(a)==set(b)

or

from collections import Counter

Counter(a)==Counter(b)

Why? and is there anything that runs faster?

When you want to benchmark simple expressions like these, you can use the timeit module. Or better yet, if you have IPython installed, you can simply use the timeit magic command, for example:

%timeit Counter(a)==Counter(b)

The set comparison would run faster. There are two main reasons for this:

1) There is more information in Counter() compared to set()

2) There are more operations required to build up a Counter() object compare to a set() .

Roughly speaking, Counter() is a dictionary, which consist of key value pairs. In Counter the values have the special meaning of counting the number of occurrences of the key. By contrast, set() only contains information about the keys.


EDIT

When it comes to addressing the question of which one should you use, this depends on what are you trying to achieve, given that the two expressions are not equivalent:

from collections import Counter

a = "hello world"
b = "world helo"

set(a) == set(b)
# True

Counter(a) == Counter(b)
# False

Therefore it is hard to give more hints in this direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM