Skip to content

Instantly share code, notes, and snippets.

@vedang
Created August 7, 2011 12:57
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 vedang/1130352 to your computer and use it in GitHub Desktop.
Save vedang/1130352 to your computer and use it in GitHub Desktop.
a simple speed test for python switching
from timeit import Timer
from random import randint
def switch_if():
value = randint(1, 10)
if value == 1:
return '1'
elif value == 2:
return '2'
elif value == 3:
return '3'
elif value == 4:
return '4'
elif value == 5:
return '5'
elif value == 6:
return '6'
elif value == 7:
return '7'
elif value == 8:
return '8'
elif value == 9:
return '9'
else:
return '10'
def switch_map():
value = randint(1, 10)
smap = {1: '1',
2: '2',
3: '3',
4: '4',
5: '5',
6: '6',
7: '7',
8: '8',
9: '9',
10: '10'}
return smap[value]
t = Timer(setup='from __main__ import switch_if', stmt='switch_if()')
print "if - ", t.timeit()
t = Timer(setup='from __main__ import switch_map', stmt='switch_map()')
print "map - ", t.timeit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment