import time
N=10**6
M=20
def square0(x):
return x*x
def square1(x):
def dummy(y):
return y*y
return x*x
def square2(x):
def dummy1(y):
return y*y
def dummy2(y):
return y*y
return x*x
def square5(x):
def dummy1(y):
return y*y
def dummy2(y):
return y*y
def dummy3(y):
return y*y
def dummy4(y):
return y*y
def dummy5(y):
return y*y
return x*x
test01_first=True
test02_first=True
test03_first=True
test04_first=True
for j in range(M):
start=time.time()
s=0
for i in range(N):
s+=square0(i)
end=time.time()
test01_time=end-start
if test01_first:
test01_sum=test01_time
test01_sqs=0
test01_approx=test01_time
test01_cnt=1
test01_first=False
else:
test01_sum+=test01_time
test01_sqs+=(test01_time-test01_approx)**2
test01_cnt+=1
start=time.time()
s=0
for i in range(N):
s+=square1(i)
end=time.time()
test02_time=end-start
if test02_first:
test02_sum=test02_time
test02_sqs=0
test02_approx=test02_time
test02_cnt=1
test02_first=False
else:
test02_sum+=test02_time
test02_sqs+=(test02_time-test02_approx)**2
test02_cnt+=1
start=time.time()
s=0
for i in range(N):
s+=square2(i)
end=time.time()
test03_time=end-start
if test03_first:
test03_sum=test03_time
test03_sqs=0
test03_approx=test03_time
test03_cnt=1
test03_first=False
else:
test03_sum+=test03_time
test03_sqs+=(test03_time-test03_approx)**2
test03_cnt+=1
start=time.time()
s=0
for i in range(N):
s+=square5(i)
end=time.time()
test04_time=end-start
if test04_first:
test04_sum=test04_time
test04_sqs=0
test04_approx=test04_time
test04_cnt=1
test04_first=False
else:
test04_sum+=test04_time
test04_sqs+=(test04_time-test04_approx)**2
test04_cnt+=1
test01_avg=test01_sum/test01_cnt
test02_avg=test02_sum/test02_cnt
test03_avg=test03_sum/test03_cnt
test04_avg=test04_sum/test04_cnt
test01_std=((test01_sqs-(test01_sum-test01_cnt*test01_approx)**2/test01_cnt)/(test01_cnt-1))**.5
test02_std=((test02_sqs-(test02_sum-test02_cnt*test02_approx)**2/test02_cnt)/(test02_cnt-1))**.5
test03_std=((test03_sqs-(test03_sum-test03_cnt*test03_approx)**2/test03_cnt)/(test03_cnt-1))**.5
test04_std=((test04_sqs-(test04_sum-test04_cnt*test04_approx)**2/test04_cnt)/(test04_cnt-1))**.5
print("m = mean, s = standard deviation, m0 = mean of first testcase")
print("[m-3s,m+3s] is a 0.997 confidence interval if normal distributed")
print()
print("square?","{0:5} {1:7} {2:5} [{3:5},{4:5}]".format("m","s","m/m0","m-3s","m+3s"))
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))
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))
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))
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))
aW1wb3J0IHRpbWUKCk49MTAqKjYKTT0yMAoKZGVmIHNxdWFyZTAoeCk6CiAgICByZXR1cm4geCp4CgpkZWYgc3F1YXJlMSh4KToKICAgIGRlZiBkdW1teSh5KToKICAgICAgICByZXR1cm4geSp5CiAgICByZXR1cm4geCp4CiAgICAKZGVmIHNxdWFyZTIoeCk6CiAgICBkZWYgZHVtbXkxKHkpOgogICAgICAgIHJldHVybiB5KnkKICAgIGRlZiBkdW1teTIoeSk6CiAgICAgICAgcmV0dXJuIHkqeQogICAgcmV0dXJuIHgqeAoKZGVmIHNxdWFyZTUoeCk6CiAgICBkZWYgZHVtbXkxKHkpOgogICAgICAgIHJldHVybiB5KnkKICAgIGRlZiBkdW1teTIoeSk6CiAgICAgICAgcmV0dXJuIHkqeQogICAgZGVmIGR1bW15Myh5KToKICAg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