【发布时间】:2019-07-22 07:31:01
【问题描述】:
我已经阅读了 Stackoverflow 中的几个线程。许多人声称使用 dict 比 Python 中的 if-elif-else 语句更快。我使用 Python 3.7.4 做了一些测试,得到了不同的结果。
我已经构建了一个测试脚本。我使用 timeit 来测量我的代码。我正在构建一个伪代码解释器。我的解释器使用 if-elif-else 语句来查找 python 过程来解释特定的伪代码。但我想转为 dict 批准并进行一些测试。
from timeit import Timer
def add(x, y):
return x + y
def sub(x, y):
return x - y
def mul(x, y):
return x * y
def div(x, y):
return x / y
def foo1(oper, a, b):
return funcs[oper](a, b)
def foo2(oper, a, b):
if oper == 'add':
return a + b
elif oper == 'sub':
return a - b
elif oper == 'mul':
return a * b
else:
return a / b
def foo3(oper, a, b):
subfuncs[oper](a, b)
funcs = {'add': lambda x, y: x + y,
'sub': lambda x, y: x - y,
'mul': lambda x, y: x * y,
'div': lambda x, y: x / y}
subfuncs = {'add': add,
'sub': sub,
'mul': mul,
'div': div}
times_to_run = 10000000
t1 = Timer("foo1('div', 3, 2)", "from __main__ import foo1")
t2 = Timer("foo2('div', 3, 2)", "from __main__ import foo2")
t3 = Timer("foo3('div', 3, 2)", "from __main__ import foo3")
tot1 = t1.timeit(times_to_run)
tot2 = t2.timeit(times_to_run)
tot3 = t3.timeit(times_to_run)
print("Time for foo1 is: {:4.2f}".format(tot1))
print("Time for foo2 is: {:4.2f}".format(tot2))
print("Time for foo3 is: {:4.2f}".format(tot3))
if tot1 > tot2:
res1 = 'slower'
times1 = '{:6.2f}'.format((tot1 / tot2 - 1) * 100)
elif tot1 < tot2:
res1 = 'faster'
times1 = '{:6.2f}'.format((tot2 / tot1 - 1) * 100)
else:
res1 = 'equal'
times1 = ''
print("\nfoo1 is {}% {} in comparison to foo2".format(times1, res1))
if tot2 > tot3:
res2 = 'slower'
times2 = '{:6.2f}'.format((tot2 / tot3 - 1) * 100)
elif tot2 < tot3:
res2 = 'faster'
times2 = '{:6.2f}'.format((tot3 / tot2 - 1) * 100)
else:
res2 = 'equal'
times2 = ''
print("foo2 is {}% {} in comparison to foo3".format(times2, res2))
if tot1 > tot3:
res3 = 'slower'
times3 = '{:6.2f}'.format((tot1 / tot3 - 1) * 100)
elif tot1 < tot3:
res3 = 'faster'
times3 = '{:6.2f}'.format((tot3 / tot1 - 1) * 100)
else:
res3 = 'equal'
times3 = ''
print("foo1 is {}% {} in comparison to foo3".format(times3, res3))
我原以为 foo3 会是最快的函数,而 foo2 每次都是最快的。我总是得到与此类似的输出,并且输出始终与此输出一致:
Time for foo1 is: 3.35
Time for foo2 is: 2.99
Time for foo3 is: 3.06
foo1 is 12.18% slower in comparison to foo2
foo2 is 2.51% faster in comparison to foo3
foo1 is 9.44% slower in comparison to foo3
我的问题是为什么具有 if-elif-else 语句的 foo2 比使用函数字典的 foo3 更快?
附言。这不是我的实际代码。我正在测试哪种方法会更快。
【问题讨论】:
-
你使用什么 CPU/Python?在我的 AMD 2400G/Python 3.6.8 上
foo1最快Time for foo1 is: 1.50 Time for foo2 is: 1.52 Time for foo3 is: 1.59 -
我有一台 Xeon E5-2680 @2.70 GHz,Python 3.7.4 上的 16GB RAM
标签: python-3.x function dictionary if-statement lambda