gpt4 book ai didi

python - 一次加法需要多少 CPU 周期?

转载 作者:太空狗 更新时间:2023-10-30 01:50:36 28 4
gpt4 key购买 nike

我想测量在 Python 3 中执行加法运算所需的时钟周期数。

我写了一个程序来计算加法运算的平均值:

from timeit import timeit

def test(n):
for i in range(n):
1 + 1

if __name__ == '__main__':

times = {}
for i in [2 ** n for n in range(10)]:
t = timeit.timeit("test(%d)" % i, setup="from __main__ import test", number=100000)
times[i] = t
print("%d additions takes %f" % (i, t))

keys = sorted(list(times.keys()))

for i in range(len(keys) - 2):
print("1 addition takes %f" % ((times[keys[i+1]] - times[keys[i]]) / (keys[i+1] - keys[i])))

输出:

16 additions takes 0.288647
32 additions takes 0.422229
64 additions takes 0.712617
128 additions takes 1.275438
256 additions takes 2.415222
512 additions takes 5.050155
1024 additions takes 10.381530
2048 additions takes 21.185604
4096 additions takes 43.122559
8192 additions takes 88.323853
16384 additions takes 194.353927
1 addition takes 0.008292
1 addition takes 0.010068
1 addition takes 0.008654
1 addition takes 0.010318
1 addition takes 0.008349
1 addition takes 0.009075
1 addition takes 0.008794
1 addition takes 0.008905
1 addition takes 0.010293
1 addition takes 0.010413
1 addition takes 0.010551
1 addition takes 0.010711
1 addition takes 0.011035

因此根据此输出,一次加法大约需要 0.0095 微秒。正在关注this page说明 我计算出一次加法需要 25 个 CPU 周期。这是正常值吗?为什么?因为汇编指令 ADD 只需要 1-2 个 CPU 周期。

最佳答案

您正在为函数调用 (test())、for 循环和对 range() 的调用计时。添加根本没有定时。

def test(n):
for i in range(n):
1 + 1

import dis
dis.dis(test)

这是您的测试函数的字节码(不包括对 test() 的调用):

  2           0 SETUP_LOOP              24 (to 27)
3 LOAD_GLOBAL 0 (range)
6 LOAD_FAST 0 (n)
9 CALL_FUNCTION 1
12 GET_ITER
>> 13 FOR_ITER 10 (to 26)
16 STORE_FAST 1 (i)

3 19 LOAD_CONST 2 (2) ****
22 POP_TOP
23 JUMP_ABSOLUTE 13
>> 26 POP_BLOCK
>> 27 LOAD_CONST 0 (None)
30 RETURN_VALUE

**** 注意,添加是在编译时完成的。相当多的其他语言及其编译器会这样做,包括 C。但是,标准很少定义 1 + 1 实际完成的时间,因此它通常取决于实现。

编辑:

您的 timeit 函数调用可能是这样的:

    t = timeit("x += 1", setup="x = 1", number=100000)

我们可以创建一个虚拟函数来检查操作:

def myfunc(x):
x += 1

import dis
dis.dis(myfunc)

做出这样的改变会得到:

1 additions takes 0.008976
2 additions takes 0.007419
4 additions takes 0.007282
8 additions takes 0.007693
16 additions takes 0.007026
32 additions takes 0.007793
64 additions takes 0.010168
128 additions takes 0.008124
256 additions takes 0.009064
512 additions takes 0.007256
1 addition takes -0.001557
1 addition takes -0.000068
1 addition takes 0.000103
1 addition takes -0.000083
1 addition takes 0.000048
1 addition takes 0.000074
1 addition takes -0.000032
1 addition takes 0.000007

26 0 LOAD_FAST 0 (x)
3 LOAD_CONST 1 (1)
6 INPLACE_ADD
7 STORE_FAST 0 (x)
10 LOAD_CONST 0 (None)
13 RETURN_VALUE

请注意,x += 1 是一个 INPLACE_ADD,不同于 x = x + 1,后者是一个 BINARY_ADD。因此,您需要决定要测量哪个 OPCode。

关于python - 一次加法需要多少 CPU 周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36338296/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com