[issue23486] Enum comparisons are 20x slower than comparing equivalent ints

Craig Holmquist report at bugs.python.org
Thu Feb 19 22:59:13 CET 2015


New submission from Craig Holmquist:

Running the attached test script:

$ time python test.py enum

real	0m6.546s
user	0m6.530s
sys	0m0.007s
$ time python test.py int

real	0m0.384s
user	0m0.377s
sys	0m0.000s

I encountered this with a script that yielded a sequence of objects (potentially a few hundred thousand of them) and categorized them with instances of an Enum subclass.  The consumer of that iteration processes each object with a switch-case-like comparison of the category, checking it sequentially against each instance of the Enum.  This seems like a fairly common use case.

>From cProfile it looks like EnumMeta.__getattr__ and _is_dunder are the main bottlenecks:

[...]
      7/1    0.000    0.000    0.000    0.000 abc.py:194(__subclasscheck__)
        1    0.000    0.000    0.001    0.001 enum.py:1(<module>)
        3    0.000    0.000    0.000    0.000 enum.py:132(<genexpr>)
  2000021    0.988    0.000    0.988    0.000 enum.py:16(_is_dunder)
       19    0.000    0.000    0.000    0.000 enum.py:24(_is_sunder)
  2000002    1.825    0.000    2.813    0.000 enum.py:241(__getattr__)
       17    0.000    0.000    0.000    0.000 enum.py:282(__setattr__)
        3    0.000    0.000    0.000    0.000 enum.py:342(_get_mixins_)
        3    0.000    0.000    0.000    0.000 enum.py:387(_find_new_)
[...]

----------
components: Library (Lib)
files: test.py
messages: 236234
nosy: craigh
priority: normal
severity: normal
status: open
title: Enum comparisons are 20x slower than comparing equivalent ints
type: performance
versions: Python 3.4
Added file: http://bugs.python.org/file38177/test.py

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue23486>
_______________________________________


More information about the Python-bugs-list mailing list