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

Python Optimizations #1

Open
grantjenks opened this issue Apr 19, 2016 · 1 comment
Open

Python Optimizations #1

grantjenks opened this issue Apr 19, 2016 · 1 comment

Comments

@grantjenks
Copy link

You can make Python about 2x faster with these changes:

values = [value % maxInt for value in xrange(warmUpIterations)]
values = [(value, value * 2, value + 1) for value in values]
map_get = map.get

# for i in xrange(warmUpIterations):
#     num1 = i % maxInt
#     num2 = num1 * 2
#     num3 = num1 + 1
for num1, num2, num3 in values:
    map[num1] = num2
    value = map_get(num3)
    # if value:
    #     # Make sure we do something with the result so that it is not optimized away
    #     sum += value

What changed:

  1. Move the math out of the loop. You want to test hash table performance not integer math.
  2. Replace map.get with map_get to avoid the method lookup.
  3. Skip summing values. Python's dead-code optimizations are not very aggressive. The loop will stay.
@nfergu
Copy link
Owner

nfergu commented Apr 19, 2016

Great, thanks @grantjenks. I've referenced this issue from the main docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants