numpy数组及处理:效率对比
2021-05-22 01:28
标签:分享 定义 处理 src code nbsp word [] new numpy数组及处理:效率对比 标签:分享 定义 处理 src code nbsp word [] new 原文地址:https://www.cnblogs.com/SpaldingWen/p/9737971.htmldef
Sum
(n):
#定义一个函数(注意:格式对齐,否则会出错)
a
=
list
(
range
(n))
b
=
list
(
range
(
0
,
50000
*
n,
5
))
c
=
[]
for
i
in
range
(
len
(a)):
c.append(a[i]
*
*
2
+
b[i]
*
*
3
)
return
c
print
(
Sum
(
20
))
import
numpy as py
def
pySum(n):
a
=
py.array(
range
(n))
b
=
py.array(
range
(
0
,
500000
*
n,n))
c
=
[]
for
i
in
range
(
len
(a)):
c.append(a[i]
*
*
2
+
b[i]
*
*
3
)
return
c
print
(pySum(
20
))
import
datetime
def
new4():
now1
=
datetime.datetime.now()
Sum
(
30000
)
now2
=
datetime.datetime.now()
pySum(
30000
)
now3
=
datetime.datetime.now()
print
(
"sum执行时间(30W数据):"
, now2
-
now1,
"\npysum数组执行时间(30W数据):"
, now3
-
now2)
new4()