fork download
  1. import time
  2.  
  3. N=10**6
  4. M=20
  5.  
  6. def square0(x):
  7. return x*x
  8.  
  9. def square1(x):
  10. def dummy(y):
  11. return y*y
  12. return x*x
  13.  
  14. def square2(x):
  15. def dummy1(y):
  16. return y*y
  17. def dummy2(y):
  18. return y*y
  19. return x*x
  20.  
  21. def square5(x):
  22. def dummy1(y):
  23. return y*y
  24. def dummy2(y):
  25. return y*y
  26. def dummy3(y):
  27. return y*y
  28. def dummy4(y):
  29. return y*y
  30. def dummy5(y):
  31. return y*y
  32. return x*x
  33.  
  34. test01_first=True
  35. test02_first=True
  36. test03_first=True
  37. test04_first=True
  38.  
  39. for j in range(M):
  40. start=time.time()
  41. s=0
  42. for i in range(N):
  43. s+=square0(i)
  44. end=time.time()
  45. test01_time=end-start
  46. if test01_first:
  47. test01_sum=test01_time
  48. test01_sqs=0
  49. test01_approx=test01_time
  50. test01_cnt=1
  51. test01_first=False
  52. else:
  53. test01_sum+=test01_time
  54. test01_sqs+=(test01_time-test01_approx)**2
  55. test01_cnt+=1
  56.  
  57. start=time.time()
  58. s=0
  59. for i in range(N):
  60. s+=square1(i)
  61. end=time.time()
  62. test02_time=end-start
  63. if test02_first:
  64. test02_sum=test02_time
  65. test02_sqs=0
  66. test02_approx=test02_time
  67. test02_cnt=1
  68. test02_first=False
  69. else:
  70. test02_sum+=test02_time
  71. test02_sqs+=(test02_time-test02_approx)**2
  72. test02_cnt+=1
  73.  
  74. start=time.time()
  75. s=0
  76. for i in range(N):
  77. s+=square2(i)
  78. end=time.time()
  79. test03_time=end-start
  80. if test03_first:
  81. test03_sum=test03_time
  82. test03_sqs=0
  83. test03_approx=test03_time
  84. test03_cnt=1
  85. test03_first=False
  86. else:
  87. test03_sum+=test03_time
  88. test03_sqs+=(test03_time-test03_approx)**2
  89. test03_cnt+=1
  90.  
  91. start=time.time()
  92. s=0
  93. for i in range(N):
  94. s+=square5(i)
  95. end=time.time()
  96. test04_time=end-start
  97. if test04_first:
  98. test04_sum=test04_time
  99. test04_sqs=0
  100. test04_approx=test04_time
  101. test04_cnt=1
  102. test04_first=False
  103. else:
  104. test04_sum+=test04_time
  105. test04_sqs+=(test04_time-test04_approx)**2
  106. test04_cnt+=1
  107.  
  108. test01_avg=test01_sum/test01_cnt
  109. test02_avg=test02_sum/test02_cnt
  110. test03_avg=test03_sum/test03_cnt
  111. test04_avg=test04_sum/test04_cnt
  112. test01_std=((test01_sqs-(test01_sum-test01_cnt*test01_approx)**2/test01_cnt)/(test01_cnt-1))**.5
  113. test02_std=((test02_sqs-(test02_sum-test02_cnt*test02_approx)**2/test02_cnt)/(test02_cnt-1))**.5
  114. test03_std=((test03_sqs-(test03_sum-test03_cnt*test03_approx)**2/test03_cnt)/(test03_cnt-1))**.5
  115. test04_std=((test04_sqs-(test04_sum-test04_cnt*test04_approx)**2/test04_cnt)/(test04_cnt-1))**.5
  116. print("m = mean, s = standard deviation, m0 = mean of first testcase")
  117. print("[m-3s,m+3s] is a 0.997 confidence interval if normal distributed")
  118. print()
  119. print("square?","{0:5} {1:7} {2:5} [{3:5},{4:5}]".format("m","s","m/m0","m-3s","m+3s"))
  120. print("square0","{0:5.3f} {1:7.5f} {2:5.3f} [{3:5.3f},{4:5.3f}]".format(test01_avg,test01_std,test01_avg/test01_avg,test01_avg-3*test01_std,test01_avg+3*test01_std))
  121. print("square1","{0:5.3f} {1:7.5f} {2:5.3f} [{3:5.3f},{4:5.3f}]".format(test02_avg,test02_std,test02_avg/test01_avg,test02_avg-3*test02_std,test02_avg+3*test02_std))
  122. print("square2","{0:5.3f} {1:7.5f} {2:5.3f} [{3:5.3f},{4:5.3f}]".format(test03_avg,test03_std,test03_avg/test01_avg,test03_avg-3*test03_std,test03_avg+3*test03_std))
  123. print("square5","{0:5.3f} {1:7.5f} {2:5.3f} [{3:5.3f},{4:5.3f}]".format(test04_avg,test04_std,test04_avg/test01_avg,test04_avg-3*test04_std,test04_avg+3*test04_std))
  124.  
  125.  
Time limit exceeded #stdin #stdout 5s 10088KB
stdin
Standard input is empty
stdout
Standard output is empty