我正在尝试计算我的 CPU 每秒可以执行的浮点操作数,所以我编写了以下程序:
import time
import sys
from multiprocessing import Process
numOfLoops = 10000000
if (len(sys.argv) != 2):
print "Correct Usage: python cpu.py 'no_of_processes'"
sys.exit()
#function for each process
def fpOps():
a = float(0.0)
for i in xrange(0, numOfLoops):
a += 0.5
#print a #for debugging purpose
processes = []
numOfProcesses = int(sys.argv[1])
numOfOps = numOfLoops * numOfProcesses
for i in xrange(0, numOfProcesses):
processes.append(Process(target=fpOps))
start = time.time()
for process in processes:
process.start()
for process in processes:
process.join()
end = time.time()
execTime = end - start
print "Exec Time " , execTime
FLOPS = (float(numOfOps/execTime))
print "FLOPS : " , FLOPS
该程序计算执行“n”次浮点运算所需的时间(execTime),“n / execTime”为我提供了我想要的 FLOPS 值。
但是,随着进程数量的增加,FLOPS 值会降低。我期望它会减少,因为这些进程每秒执行更多的操作,因为它们是并行运行的。我究竟做错了什么??