Created
May 15, 2014 11:02
-
-
Save akx/16827e93986b1cb5d41f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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