Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

operator.attrgetter is slower than a lambda #61396

Closed
florentx mannequin opened this issue Feb 12, 2013 · 3 comments
Closed

operator.attrgetter is slower than a lambda #61396

florentx mannequin opened this issue Feb 12, 2013 · 3 comments
Labels
performance Performance or resource usage

Comments

@florentx
Copy link
Mannequin

florentx mannequin commented Feb 12, 2013

BPO 17194
Nosy @florentx, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-02-12.22:30:54.343>
created_at = <Date 2013-02-12.16:43:15.354>
labels = ['invalid', 'performance']
title = 'operator.attrgetter is slower than a lambda'
updated_at = <Date 2013-02-12.22:30:54.341>
user = 'https://github.com/florentx'

bugs.python.org fields:

activity = <Date 2013-02-12.22:30:54.341>
actor = 'flox'
assignee = 'none'
closed = True
closed_date = <Date 2013-02-12.22:30:54.343>
closer = 'flox'
components = []
creation = <Date 2013-02-12.16:43:15.354>
creator = 'flox'
dependencies = []
files = []
hgrepos = []
issue_num = 17194
keywords = []
message_count = 3.0
messages = ['181967', '181978', '181979']
nosy_count = 2.0
nosy_names = ['flox', 'serhiy.storchaka']
pr_nums = []
priority = 'low'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'performance'
url = 'https://bugs.python.org/issue17194'
versions = []

@florentx
Copy link
Mannequin Author

florentx mannequin commented Feb 12, 2013

When two implementations give the same result, I use to run micro benchmarks to give me an hint.

I just noticed that attrgetter is slower than a lambda here:

$ python3.3 -m timeit -s 'from operator import attrgetter; n1 = attrgetter("__name__"); n2 = lambda s: s.__name__' 'rv = n1(int)'
1000000 loops, best of 3: 0.275 usec per loop
$ python3.3 -m timeit -s 'from operator import attrgetter; n1 = attrgetter("__name__"); n2 = lambda s: s.__name__' 'rv = n2(int)'
1000000 loops, best of 3: 0.347 usec per loop

(verified with 2.6, 2.7 and 3.3. But for 2.5 attrgetter is faster)

The function operator.itemgetter does not have same issue.
$ python3.3 -m timeit -s 'from operator import itemgetter; n1 = itemgetter("foot"); n2 = lambda s: s["foot"]; d = {"foot": 42}' 'rv = n1(d)'
10000000 loops, best of 3: 0.122 usec per loop
$ python3.3 -m timeit -s 'from operator import itemgetter; n1 = itemgetter("foot"); n2 = lambda s: s["foot"]; d = {"foot": 42}' 'rv = n2(d)'
10000000 loops, best of 3: 0.176 usec per loop

@florentx florentx mannequin added the performance Performance or resource usage label Feb 12, 2013
@florentx florentx mannequin changed the title operator.attrgetter is slow operator.attrgetter is slower than a lambda Feb 12, 2013
@serhiy-storchaka
Copy link
Member

Are you sure? 0.275 < 0.347

@florentx
Copy link
Mannequin Author

florentx mannequin commented Feb 12, 2013

You're right. I've misinterpreted the figures.

Only 2.6 and 2.7 are affected --> closing the issue.

@florentx florentx mannequin closed this as completed Feb 12, 2013
@florentx florentx mannequin added the invalid label Feb 12, 2013
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Performance or resource usage
Projects
None yet
Development

No branches or pull requests

1 participant