Skip to content

Instantly share code, notes, and snippets.

@bergantine
Last active October 1, 2015 17:17
Show Gist options
  • Select an option

  • Save bergantine/2029226 to your computer and use it in GitHub Desktop.

Select an option

Save bergantine/2029226 to your computer and use it in GitHub Desktop.
Time the execution of a method in Python using timeit.Timer.timeit(). #python
"""
Look up all Users associated with a particular badge.
"""
import timeit
s = """\
from profiles.models import Badge, UserProfile
badges = Badge.objects.all().only('name')
for b in badges:
print b.name
print UserProfile.objects.filter(badges=b).count()
"""
timeit.Timer(stmt=s).timeit(number=1)
# Executes in 0.0585939884185791 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment