Skip to content

Instantly share code, notes, and snippets.

@akx
Created May 15, 2014 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akx/16827e93986b1cb5d41f to your computer and use it in GitHub Desktop.
Save akx/16827e93986b1cb5d41f to your computer and use it in GitHub Desktop.
from random import random
def maybe_raise(chance, n):
for x in xrange(n):
try:
if random() < chance:
raise ValueError("derp")
except ValueError:
pass
from timeit import timeit
number = 5000
for c in [1, 0.9, 0.3, 0.1, 0]:
print "%d%% raise exceptions: time %.2f sec" % (c * 100, timeit(lambda:maybe_raise(c, 1000), number=number))
100% raise exceptions: time 5.79 sec
90% raise exceptions: time 4.01 sec
30% raise exceptions: time 1.71 sec
10% raise exceptions: time 0.91 sec
0% raise exceptions: time 0.58 sec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment