所以我正在使用嵌套的while循环来尝试迭代这些函数,我不明白为什么在第二个循环和第二个循环等循环为什么它一直打印出相同的数字它应该更大每个后续循环。
老实说,我不知道究竟是什么搞砸了,如果有人能指出我非常欣赏它的问题,那么它很可能很小。
import math
p = 1
n = 10
while n < 321:
p = 1
while p < 129:
T_serial = n ^ 2
T_parallel = (T_serial / p) + math.log(p)
speed = T_serial / T_parallel
effic = speed / p
result = speed / effic
print(result)
p *= 2
n += 10
答案 0 :(得分:1)
以下显示了正在发生的事情:
import math
p = 1.0
n = 10.0
print "n\tp\tresult"
while n < 321.0:
p = 1
while p < 129.0:
#print "n", n, "p", p
T_serial = n**2.0
T_parallel = (T_serial / p) + math.log(p)
speed = T_serial / T_parallel
effic = speed / p
result = speed / effic
print "%s\t%s\t%s" % (n, p, result)
p *= 2.0
n += 10.0
输出:
n p result
10.0 1 1.0
10.0 2.0 2.0
10.0 4.0 4.0
10.0 8.0 8.0
10.0 16.0 16.0
10.0 32.0 32.0
10.0 64.0 64.0
10.0 128.0 128.0
20.0 1 1.0
20.0 2.0 2.0
20.0 4.0 4.0
20.0 8.0 8.0
20.0 16.0 16.0
20.0 32.0 32.0
20.0 64.0 64.0
20.0 128.0 128.0
30.0 1 1.0
30.0 2.0 2.0
30.0 4.0 4.0
30.0 8.0 8.0
30.0 16.0 16.0
30.0 32.0 32.0
30.0 64.0 64.0
30.0 128.0 128.0
40.0 1 1.0
[etc...]
查看effic
和result
的等式。最后你刚刚做了
results = speed / effic = speed / (speed / p) = p
所以难怪它不是你所期望的。您只需每次打印p
。
答案 1 :(得分:0)
你的第一个while循环中有一个缩进错误。
import math
p = 1
n = 10
while n < 321:
p = 1
while p < 129:
T_serial = n ^ 2
T_parallel = (T_serial / p) + math.log(p)
speed = T_serial / T_parallel
effic = speed / p
result = speed / effic
print(result)
p *= 2
n += 10