如何在单个脚本文件中找到最快的算法?
我有很多问题的答案,我想测试哪种算法/答案是最好的。计算大o符号的复杂性非常困难(必须手工做,用笔和纸做),因此我决定计算编译时间(我可以使用编码)。问题即使在相同的代码中,编译时间也在不断变化,因此我无法确定哪种算法是最优化的(最快)。以下是我的简单代码:
import time
from datetime import datetime
st = time.time()
#st = time.process_time()
#st = datetime.now()
for i in range(6000):
print(i)
et = time.time()
#et = time.process_time()
#et = datetime.now()
#print(et-st)
print(format(et-st,".7f"))
每次运行时结果都不同。我尝试了dateTime
,process_time
,以及cprofile
,但它们遇到了同样的问题。所以我的问题是,是否有任何简单而快速的方法来比较不同的算法?我不想使用大o符号,因为它不是有效的(非常困难,需要很长时间才能计算)。
以下是我的实际代码,问题是在产品最大的列表中找到一对数字。例如,在列表[5,1,2,4,3]中,一对数字为5、3,它具有最大的产品(5*3 = 15)。我有3个解决方案,1使用嵌套环,1使用itertool.combination,1使用排序。我所得到的只是0.000 ...第二个用于不同输入(我测试了所有3种方法,并获得了所有3个方法的正确结果)。
import time
import cProfile
import itertools
n=int(input())
list0=[]
for i in range(n):
ele = int(input())
list0.append(ele)
def pairwise1(list0): #Using nested loop for each value in a list
result = 0
for x in range(n):
for y in range(x+1,n,1):
if list0[x]*list0[y] >= result:
result = list0[x]*list0[y]
return result
def pairwise2(list0): #using itertools
list1 = list(itertools.combinations(list0,2)) # for example list1 = [(1, 6), (1, 2), (1, 4), (1, 3), (6, 2), (6, 4), (6, 3), (2, 4), (2, 3), (4, 3)]
sum0 = 0
for i in list1:
if sum(i)>=sum0:
sum0=sum(i)
result = i[0]*i[1]
return result
def pairwise3(list0): #using sorted
list0.sort(reverse=True)
result = list0[0]*list0[1]
return result
# cProfile.run('pairwise1(list0)') #0.0000 second, didn't work
# cProfile.run('pairwise2(list0)') #0.0000 second, didn't work
# cProfile.run('pairwise3(list0)') #0.0000 second, didn't work
st1 = time.time()
pairwise1(list0)
et1 = time.time()
print(format(et1-st1,".20f")) #still 0.0000... second
st2 = time.time()
pairwise2(list0)
et2 = time.time()
print(format(et2-st2,".20f")) #still 0.0000... second
st3 = time.time()
pairwise3(list0)
et3 = time.time()
print(format(et3-st3,".20f")) #still 0.0000... second
好的,我弄清楚了。输入太小了,在稍微更改我的代码之后,输入一个大列表(随机的10000个元素)都变得清晰,排序方法始终是最快的,只需0.001秒,然后进入嵌套循环方法,最后,iTer,方法最长。我多次重复测试,结果不会改变太大。
I have many answers to a problem and I want to test which algorithm/answer is the best. Calculating big O notation complexity is very hard (have to do it by hand, with pen, and paper) so I decide to calculate compile time instead (which I can use coding). The problem is somehow even for the same code, the compile time keeps changing so I can't decide which algorithm is the most optimized (fastest). Below is my simple code:
import time
from datetime import datetime
st = time.time()
#st = time.process_time()
#st = datetime.now()
for i in range(6000):
print(i)
et = time.time()
#et = time.process_time()
#et = datetime.now()
#print(et-st)
print(format(et-st,".7f"))
The results are different every time I run. I tried datetime
, process_time
, and also cProfile
but they encounter the same problem. So my question is, is there any simple and fast way to compare different algorithms? I don't want to use big O notation since it's not efficient (very hard and takes a long time to calculate).
Below is my actual code, the problem is to Find the pair of numbers in a list whose product is the largest. For example in a list [5,1,2,4,3] then the pair of numbers is 5, 3 which has the largest product (5*3=15). I have 3 solutions, 1 using nested loop, 1 using itertool.combination, 1 using sorted. Somehow all I get is 0.000... second for different input (I tested all 3 methods and get the right result for all 3).
import time
import cProfile
import itertools
n=int(input())
list0=[]
for i in range(n):
ele = int(input())
list0.append(ele)
def pairwise1(list0): #Using nested loop for each value in a list
result = 0
for x in range(n):
for y in range(x+1,n,1):
if list0[x]*list0[y] >= result:
result = list0[x]*list0[y]
return result
def pairwise2(list0): #using itertools
list1 = list(itertools.combinations(list0,2)) # for example list1 = [(1, 6), (1, 2), (1, 4), (1, 3), (6, 2), (6, 4), (6, 3), (2, 4), (2, 3), (4, 3)]
sum0 = 0
for i in list1:
if sum(i)>=sum0:
sum0=sum(i)
result = i[0]*i[1]
return result
def pairwise3(list0): #using sorted
list0.sort(reverse=True)
result = list0[0]*list0[1]
return result
# cProfile.run('pairwise1(list0)') #0.0000 second, didn't work
# cProfile.run('pairwise2(list0)') #0.0000 second, didn't work
# cProfile.run('pairwise3(list0)') #0.0000 second, didn't work
st1 = time.time()
pairwise1(list0)
et1 = time.time()
print(format(et1-st1,".20f")) #still 0.0000... second
st2 = time.time()
pairwise2(list0)
et2 = time.time()
print(format(et2-st2,".20f")) #still 0.0000... second
st3 = time.time()
pairwise3(list0)
et3 = time.time()
print(format(et3-st3,".20f")) #still 0.0000... second
Ok, I figure it out. The input is too small, after changing my code a bit, inputting a large list (10000 elements randomly) all become clear, sorting method is always the fastest, taking only 0.001 seconds, then come to the nested loop method, finally, the iter method takes the longest. I repeat the test many times and the result won't change much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论