[issue23486] Enum comparisons are 20x slower than comparing equivalent ints
Changes by Ethan Furman et...@stoneleaf.us: -- nosy: +ethan.furman ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23486 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23486] Enum comparisons are 20x slower than comparing equivalent ints
New submission from Craig Holmquist: Running the attached test script: $ time python test.py enum real0m6.546s user0m6.530s sys 0m0.007s $ time python test.py int real0m0.384s user0m0.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/10.0000.0000.0000.000 abc.py:194(__subclasscheck__) 10.0000.0000.0010.001 enum.py:1(module) 30.0000.0000.0000.000 enum.py:132(genexpr) 2210.9880.0000.9880.000 enum.py:16(_is_dunder) 190.0000.0000.0000.000 enum.py:24(_is_sunder) 2021.8250.0002.8130.000 enum.py:241(__getattr__) 170.0000.0000.0000.000 enum.py:282(__setattr__) 30.0000.0000.0000.000 enum.py:342(_get_mixins_) 30.0000.0000.0000.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 rep...@bugs.python.org http://bugs.python.org/issue23486 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23486] Enum comparisons are 20x slower than comparing equivalent ints
Ethan Furman added the comment: Craig Holmquist wrote: - 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. So for every object you compare against every Enum member? Is there a reason you don't just use the lookup capability? class Category(Enum): tiny = 1 medium = 2 large = 3 cat = Category(obj.category) # assumes obj.category is 1, 2, or 3 -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23486 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23486] Enum comparisons are 20x slower than comparing equivalent ints
Craig Holmquist added the comment: I may not have been clear before. What I mean is, code like this: for obj in get_objects(): if obj.category == Cat.cat1: #do something elif obj.category == Cat.cat2: #do something else elif obj.category == Cat.cat3 or obj.category == Cat.cat4: #... obj.category is already an instance of Cat, in other words. The consumer is using it to determine what to do with each obj. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23486 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue23486] Enum comparisons are 20x slower than comparing equivalent ints
Craig Holmquist added the comment: It seems like performance is drastically improved by doing this: class Category(Enum): tiny = 1 medium = 2 large = 3 tiny = Category.tiny medium = Category.medium In other words, resolving Category.tiny and Category.medium is what's slow. The comparison itself is very fast. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue23486 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com