该测试是单线程的,它调用函数“计算”的1000倍。此函数在约0.6秒内将0到10,000,000之间的所有整数相加。它返回执行此计算所需的实际墙时间。该测试会跟踪最短时间,时间总和和时间总和的平方。调用1000次后,将计算并打印均值,标准差和忙于其他操作的值。
忙于其他事情是:sum_time-最短时间*循环数
忙于在MacOS,Linux / Ubuntu和iPad上进行操作的价值从0.493%到4.050%。这是操作系统的预期费用。
Windows上的结果不断提高(最佳和最差从10开始):
>python test.py
Busy with something else 9.723% or 58.734 from 604.051 seconds
min = 0.545 mean = 0.604 stdev = 0.133
>python test.py
Busy with something else 16.230% or 105.709 from 651.320 seconds
min = 0.546 mean = 0.651 stdev = 0.194
我的Windows计算机信息:
2.4 GHz Intel Core i5-6300U, Memory 4 GB
Microsoft Windows [Version 10.0.17763.379]
Python version 3.7.2, Executing in 64bit
测试:
import time
import sys
def calculate():
start = time.perf_counter()
result = 0
for i in range(10000000):
result += i
stop = time.perf_counter()
return stop - start
number_of_loops = 1000
min_time = sys.maxsize
sum_time = 0.0
sum_time_square = 0.0
for _ in range(number_of_loops):
t = calculate()
min_time = min(min_time, t)
sum_time += t
sum_time_square += t ** 2
mean = sum_time / number_of_loops
stdev = ((sum_time_square + number_of_loops * mean**2 - 2.0 * mean * sum_time) /
(number_of_loops - 1.0)) ** 0.5
busy_with_else = sum_time - min_time * number_of_loops
print('Busy with something else %.3f%% or %.3f from %.3f seconds' %
(100.0 * busy_with_else / sum_time, busy_with_else, sum_time))
print('min = %.3f mean = %.3f stdev = %.3f' % (min_time, mean, stdev))
在测试之前处于空闲状态的系统上执行的所有测试。在Windows上进行类似的C ++测试的结果是0.626%至2.059%。因此,这不是Windows的责任。
这仅仅是我的软件/硬件,还是可以复制和/或解释?