Skip to content

Instantly share code, notes, and snippets.

@bluepost59

bluepost59/sumup.py

Created Dec 6, 2017
Embed
What would you like to do?
いろんな方法で累積和を計算して時間を計測する
import time as t
import numpy as np
import sys
smp_num = 10000
mydata = [float(i) for i in range(smp_num)]
res = [0.0 for i in range(smp_num)]
def integ_for():
res[0] = mydata[0]
for i in range(len(mydata)-1):
res[i+1] = res[i] + mydata[i+1]
def integ_list():
res = [sum(mydata[:i+1]) for i in range(len(mydata))]
def integ_rec():
sys.setrecursionlimit(len(mydata)+10)
# print(sys.getrecursionlimit())
def step(myres,mymydata,i):
if i == len(mymydata)-1:
# print(myres)
pass
else:
myres[i+1] = myres[i] + mymydata[i+1]
step(myres,mymydata,i+1)
step(res,mydata,0)
def integ_cumsum():
res = np.cumsum(mydata)
if __name__ == '__main__':
time_rec = [0]*5
time_rec[0] = t.time()
integ_for()
time_rec[1] = t.time()
integ_list()
time_rec[2] = t.time()
integ_rec()
time_rec[3] = t.time()
integ_cumsum()
time_rec[4] = t.time()
time_res = np.diff(time_rec)
print('report')
print('for loop : {:0.5f} sec'.format(time_res[0]))
print('list : {:0.5f} sec'.format(time_res[1]))
print('recursive : {:0.5f} sec'.format(time_res[2]))
print('numpy.cumsum: {:0.5f} sec'.format(time_res[3]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment