Skip to content

Instantly share code, notes, and snippets.

@gj84
Last active December 19, 2015 01:09
Show Gist options
  • Save gj84/5873890 to your computer and use it in GitHub Desktop.
Save gj84/5873890 to your computer and use it in GitHub Desktop.
Comparing a simulated dict-based switch-case sentence vs. a if-else sentence in Python.
#-*- coding: utf-8 -*-
import timeit
class Datatests():
def __init__(self):
self.fns = {'ifelif': {'maximos': [],'minimos': [], 'media': []},
'ifelifcalling': {'maximos': [],'minimos': [], 'media': []},
'simulswitch': {'maximos': [],'minimos': [], 'media': []}}
def test(self,function_name,module_name,parameter,test_repetitions,function_calls):
t = timeit.Timer("%s(%d)" % (function_name,parameter),"from %s import %s" % (module_name,function_name))
times = t.repeat(test_repetitions,function_calls)
self.set_values(function_name,max(times),min(times),sum(times)/len(times))
def set_values(self,fn,maximo,minimo,media):
self.fns[fn]['maximos'].append(maximo)
self.fns[fn]['minimos'].append(minimo)
self.fns[fn]['media'].append(media)
def show(self):
for fn in ['ifelif','ifelifcalling','simulswitch']:
print " %-13s - tiempo maximo: %fs - tiempo minimo: %fs - media: %fs" % (fn,max(self.fns[fn]['maximos']),min(self.fns[fn]['minimos']),sum(self.fns[fn]['media'])/len(self.fns[fn]['media']))
#run test from diferent orders
f_names = [["ifelif","ifelifcalling","simulswitch"],
["ifelif","simulswitch","ifelifcalling"],
["ifelifcalling","ifelif","simulswitch"],
["ifelifcalling","simulswitch","ifelif"],
["simulswitch","ifelif","ifelifcalling"],
["simulswitch","ifelifcalling","ifelif"],]
test_repetitions = 2000
function_calls = 1000
#only 2 functions
module_name = "functions2"
parameter = 1 # 0 o 1
print "\n functions: %d | test repetitions: %d | function calls: %d\n" % (parameter+1,test_repetitions,function_calls)
for fnames in f_names:
datatests = Datatests()
for fn in fnames:
datatests.test(fn,module_name,parameter,test_repetitions,function_calls)
datatests.show()
#20 functions
module_name = "functions20"
parameter = 19 # 0 to 19
print "\n functions: %d | test repetitions: %d | function calls: %d\n" % (parameter+1,test_repetitions,function_calls)
for fnames in f_names:
datatests = Datatests()
for fn in fnames:
datatests.test(fn,module_name,parameter,test_repetitions,function_calls)
datatests.show()
print '\nfin test\n'
#Console output
#gj84@ubuntu:~/Escritorio/test_simulswitch_vs_ifelif$ pythontest_simulswitch_vs_if_elif.py
#functions: 2 | test repetitions: 2000 | function calls: 1000
# ifelif - tiempo maximo: 0.000356s - tiempo minimo: 0.000192s - media: 0.000214s
# ifelifcalling - tiempo maximo: 0.000578s - tiempo minimo: 0.000372s - media: 0.000394s
# simulswitch - tiempo maximo: 0.000707s - tiempo minimo: 0.000484s - media: 0.000508s
#
#functions: 20 | test repetitions: 2000 | function calls: 1000
# ifelif - tiempo maximo: 0.000866s - tiempo minimo: 0.000624s - media: 0.000658s
# ifelifcalling - tiempo maximo: 0.001006s - tiempo minimo: 0.000780s - media: 0.000793s
# simulswitch - tiempo maximo: 0.011942s - tiempo minimo: 0.001871s - media: 0.001993s
#
#fin test
def foo(): pass
def bar(): pass
def ifelif(n):
if n == 0: pass
elif n == 1: pass
def ifelifcalling(n):
if n == 0: foo()
elif n == 1: bar()
def simulswitch(n):
dic = {0:foo,1:bar}
dic[n]()
def a(): pass
def b(): pass
def c(): pass
def d(): pass
def e(): pass
def f(): pass
def g(): pass
def h(): pass
def i(): pass
def j(): pass
def k(): pass
def l(): pass
def m(): pass
def n(): pass
def o(): pass
def p(): pass
def q(): pass
def r(): pass
def s(): pass
def t(): pass
def ifelif(n):
if n == 0: pass
elif n == 1: pass
elif n == 2: pass
elif n == 3: pass
elif n == 4: pass
elif n == 5: pass
elif n == 6: pass
elif n == 7: pass
elif n == 8: pass
elif n == 9: pass
elif n == 10: pass
elif n == 11: pass
elif n == 12: pass
elif n == 13: pass
elif n == 14: pass
elif n == 15: pass
elif n == 16: pass
elif n == 17: pass
elif n == 18: pass
elif n == 19: pass
def ifelifcalling(n):
if n == 0: a()
elif n == 1: b()
elif n == 2: c()
elif n == 3: d()
elif n == 4: e()
elif n == 5: f()
elif n == 6: g()
elif n == 7: h()
elif n == 8: i()
elif n == 9: j()
elif n == 10: k()
elif n == 11: l()
elif n == 12: m()
elif n == 13: n()
elif n == 14: o()
elif n == 15: p()
elif n == 16: q()
elif n == 17: r()
elif n == 18: s()
elif n == 19: t()
def simulswitch(n):
dic = {0:a,1:b,2:c,3:d,4:e,5:f,6:g,7:h,8:i,9:j,10:k,11:l,12:m,13:n,14:o,15:p,16:q,17:r,18:s,19:t}
dic[n]()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment