Created
October 3, 2013 07:24
-
-
Save mappu/6806313 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
import time | |
import random | |
start = time.time() | |
SAMPLE_SIZE = 100000 | |
def nothing(): | |
time.time() # Man, python hates empty blocks | |
for i in xrange(SAMPLE_SIZE): | |
x = random.randrange(10) | |
if x == 0: | |
nothing() | |
elif x == 1: | |
nothing() | |
elif x == 2: | |
nothing() | |
elif x == 3: | |
nothing() | |
elif x == 4: | |
nothing() | |
elif x == 5: | |
nothing() | |
elif x == 6: | |
nothing() | |
elif x == 7: | |
nothing() | |
elif x == 8: | |
nothing() | |
elif x == 9: | |
nothing() | |
stop = time.time() | |
print("if chain: " + repr(stop - start) + "\n") | |
## | |
map = { | |
0: nothing, | |
1: nothing, | |
2: nothing, | |
3: nothing, | |
4: nothing, | |
5: nothing, | |
6: nothing, | |
7: nothing, | |
8: nothing, | |
9: nothing | |
} | |
start = time.time() | |
for i in xrange(SAMPLE_SIZE): | |
x = random.randrange(10) | |
map[x]() | |
stop = time.time() | |
print("cached map: " + repr(stop - start) + "\n") | |
## | |
start = time.time() | |
for i in xrange(SAMPLE_SIZE): | |
x = random.randrange(10) | |
{ | |
0: nothing, | |
1: nothing, | |
2: nothing, | |
3: nothing, | |
4: nothing, | |
5: nothing, | |
6: nothing, | |
7: nothing, | |
8: nothing, | |
9: nothing | |
}[x]() | |
stop = time.time() | |
print("uncached map: " + repr(stop - start) + "\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment