Reputation: 55
Enum datatypes are good abstractions for enumerable datatype like days of week, months etc. Nevertheless the simplest tests show that we pay 2.5 slower performance for such datatypes. Do we have any explanation for such behavior?
Consider two simple enums in Python
import enum
import timeit
class IntDow(enum.Enum):
MONDAY=enum.auto()
TUESDAY=enum.auto()
WEDNESDAY=enum.auto()
THURSDAY=enum.auto()
FRIDAY=enum.auto()
SATURDAY=enum.auto()
SUNDAY=enum.auto()
class StrDow(str, enum.Enum):
MONDAY="MONDAY"
TUESDAY="TUESDAY"
WEDNESDAY="WEDNESDAY"
THURSDAY="THURSDAY"
FRIDAY="FRIDAY"
SATURDAY="SATURDAY"
SUNDAY="SUNDAY"
int_mon = IntDow["MONDAY"]
int_tue = IntDow["TUESDAY"]
str_mon = StrDow["MONDAY"]
str_tue = StrDow["TUESDAY"]
raw_mon = "MONDAY"
raw_tue = "TUESDAY"
and carry out the simplest equality tests
>>> timeit.timeit(lambda: int_mon == IntDow.MONDAY, number=100000)
0.017555099999299273
>>> timeit.timeit(lambda: int_mon == int_tue, number=100000)
0.00824300000022049
>>> timeit.timeit(lambda: int_mon == IntDow.TUESDAY, number=100000)
0.018771999999444233
>>> timeit.timeit(lambda: str_mon == StrDow.MONDAY, number=100000)
0.01836639999964973
>>> timeit.timeit(lambda: str_mon == StrDow.TUESDAY, number=100000)
0.01744440000038594
>>> timeit.timeit(lambda: str_mon == str_tue, number=100000)
0.007430400000885129
>>> timeit.timeit(lambda: raw_mon == "MONDAY", number=100000)
0.007222599999295198
>>> timeit.timeit(lambda: raw_mon == "TUESDAY", number=100000)
0.00726819999908912
>>> timeit.timeit(lambda: raw_mon == raw_tue, number=100000)
0.007780600000842242
We can see that variable to constant comparison for enum
is approximately 2.5 times slower than for str
. Do we have any explanation for such behavior?
Upvotes: 4
Views: 256
Reputation: 69041
The bulk of the extra time is not spent in the equality test, but in looking up the member from the enum (i.e. IntDow.MONDAY
). In those cases where performance is critical, export the members from the enum first:
MONDAY, TUESDAY, ... = IntDow
Disclosure: I am the author of the Python stdlib Enum
, the enum34
backport, and the Advanced Enumeration (aenum
) library.
Upvotes: 3