Numpy
程序员文章站
2022-04-01 10:29:28
...
代码
from datetime import datetime
import numpy as np
from numpy.core.numeric import asanyarray
from numpy.core import umath as um
N=360000
data=range(0,N)
av=np.mean(data)
start=datetime.now()
sum=0
for i in data:
sum+=(i-av)*(i-av)
std=np.sqrt(sum/(N-1))
end=datetime.now()
print(start,":",av)
print(end,":",std)
print(end-start)
start=datetime.now()
b=np.array(data)
av=np.mean(b)
std=np.std(b,ddof=1)
end=datetime.now()
print(start,":",av)
print(end,":",std)
print(end-start)
start=datetime.now()
umr_sum = um.add.reduce
sum0=0
Nu=int(N/10)
for j in range(0,10):
x=asanyarray(b[j*Nu:j*Nu+Nu]-av)
x = um.multiply(x, x, out=x)
sum0 += umr_sum(x)
var=sum0/(N-1)
std=np.sqrt(var)
end=datetime.now()
print(start,":",av)
print(end,":",std)
print(end-start)
start=datetime.now()
x=asanyarray(b-av)
x = um.multiply(x, x, out=x)
sum0 = um.add.reduce(x)
var=sum0/(N-1)
std=np.sqrt(var)
end=datetime.now()
print(start,":",av)
print(end,":",std)
print(end-start)
运算结果
2018-10-19 19:03:43.577442 : 179999.5
2018-10-19 19:03:45.437949 : 103923.192791
0:00:01.860507
2018-10-19 19:03:45.539419 : 179999.5
2018-10-19 19:03:45.942023 : 103923.192792
0:00:00.402604
2018-10-19 19:03:46.035849 : 179999.5
2018-10-19 19:03:46.047532 : 103923.192792
0:00:00.011683
2018-10-19 19:03:46.140863 : 179999.5
2018-10-19 19:03:46.153301 : 103923.192792
0:00:00.012438
上一篇: js统一全局变量