hujilin1229 于 2016.03.21 22:54 提问

Python运行时间测试，循环步长越长，时间越短？ 1C

`````` #Test program for loop file
import time
import numpy as np
import math

def fib(n):
a, b = 1, 1
for i in range(n - 1):
b = a + b
return a

def main():
durations = []
for hbar in np.arange(10000000, 100000001, 1000000, dtype=np.int32):
totaldur = 0
for loopi in range(500):
ts = time.perf_counter()
fib(hbar)
duration = time.perf_counter() - ts
totaldur += duration
avgdur = totaldur/500
durations.append(avgdur*1000)
with open("fibtime.txt",'a') as fibtime:
for duri in range(len(durations)):
fibtime.write(durations[duri].__str__())
fibtime.write("\n")

if __name__ == "__main__":
main()
``````

0.03618908093449499
0.008038216691254783
1.666207597038485
4.930384238617884
0.004369967799217278
0.004703721429002883
1.5210544181872097
4.20149063950314
0.004469561706981651
0.003966270356412949
1.524063522666916
3.236062269760394
0.0034515751232397918
0.0033443786271689646
1.6086529619873708
3.155688465695029
0.002819800029126185
0.002822080805643168
1.4022236810744602
0.003760240211356347
0.00343789046419829
0.11917893566041116
1.6752303489246039
0.0037283093401505596
0.002540785035662907
0.348679031262936
1.8380040468578045
0.0025924826366185982
0.002516456752854168
0.6250946997414957
1.7955025367936948
0.0020215282495179565
0.002346919032092387
0.5843189772389863
1.740299382327695
0.001991117896047001
0.0019850358251076727
0.7146889229907742
1.940911922873255
0.0022093121824369177
0.0021956275233492306
0.8987650735569304
2.112751707446364
0.0020542193795094477
0.0020549796383591
1.1203485938822624
2.3569635718177153
0.002023048767185287
0.048425446927829796
1.306459197098306
0.0024974502816945687
0.0021986685587975785
0.20808968621452095
1.5845983723618033
0.0023438779967150936
0.004086391252748456
0.47692709459863636
1.7565309085130636
0.0021979082998058175
0.0033223311209553685
0.5736723124756011
1.775610364304697
0.002070184815160303
0.0020572604149435847
1.0383181858095156
2.006096515619724
0.002257208489204743
0.0021705389817014975
0.8902881875163118
2.4096305028008516
0.0020922323215089023
0.0020625822267987814
1.1149994127000369
2.289857804988195
0.0022488456418940928
0.0022655713364585495
1.3835973395502421
0.002024569284834854
0.002004802555035212
0.15670759266699008
1.434994638274496
0.0025324221885441034
0.0022321199476564857
0.3116346591360468
1.4386218331892948
0.002135567075100653
0.0020063230726918846
0.4383135482305818
1.8716264939560858
0.002254927712300514
0.0030402750918199217

2个回答

devmiao      2016.03.21 23:00
github_34379690   2016.03.22 08:26