第一种:
from datetime import datetime
#f(x)=a0+a1x+a2x*2+a3x**3+...
def f(a,x):
p=0
for i in range(0,len(a)):
p=p+a[i]*((x)**i)
a=[2,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6]
x=1
begin = datetime.now() # 获取当前datetime
beg_stamp=begin.timestamp()
for i in range(0,100):
t=f(a,x)
end = datetime.now() # 获取当前datetime
end_stamp=end.timestamp() # 把datetime转换为timestamp
print(end_stamp-beg_stamp)
第二种:
from datetime import datetime
#f(x)=a0+x(a1+x(...(an-1+x(an))))
def f(a,x):
p=a[-1]
for i in range(0,len(a)-1):
b=list(reversed(a))
p=b[i+1]+x*p
a=[2,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6,9,7,7,8,9,6,3,2,6,8,9,7,7,8,9,6]
x=1
begin = datetime.now() # 获取当前datetime
beg_stamp=begin.timestamp()
for i in range(0,100 ):
t=f(a,x)
end = datetime.now() # 获取当前datetime
end_stamp=end.timestamp() # 把datetime转换为timestamp
print(end_stamp-beg_stamp)
注意:按道理第一种的时间复杂度为n平方,第二种为n
但是做出来的是第一种时间更短 ,不知道为什么???试了很多次
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。