记录一次耗时比较久的2种方法

Posted wenwen9090

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了记录一次耗时比较久的2种方法相关的知识,希望对你有一定的参考价值。

待分析:内存与复杂度
问题:
有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
第一种解法:
#在成为领导者之前,成功的全部就是自我成长
#有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
#1.2.3.4.5,6 |1,2,4,5|1,2,5|1,5
#1,2,3,4,5
#1,2,3,4
#解决思路提供了三种情况的遍历,采用了递归的方式,但不得不说增加了很多额外变量的开销
def one_px(lst):
length=len(lst)
if(length%3==0):
newlst = queue_n(lst)
elif (length%3==1):
newlst = queue_n1(lst)
else:
newlst = queue_n2(lst)
if len(newlst)>=3:
one_px(newlst) #递归
else:
print(newlst)
return newlst


def queue_n(lst):
l=len(lst)
for i in range(l,-1,-1):
if (i+1)%3==0:
lst.remove(lst[i])
print(‘n‘)
print(lst)
return lst

def queue_n2(lst):
l=len(lst)
for i in range(l-2,-1,-1):
if (i + 1) % 3 == 0:
lst.remove(lst[i])
l = len(lst)
temp1 = lst[l - 2]
temp2 = lst[l-1]
print(lst)
for i in range(l-3,-1,-1):
lst[i+2]=lst[i]
lst[0]=temp1
lst[1]=temp2
print(‘n2‘)
print(lst)
return lst

def queue_n1(lst):
l=len(lst)
for i in range(l,-1,-1):
if (i + 1) % 3 == 0:
lst.remove(lst[i])
print(lst)
l=len(lst)
temp=lst[l-1]
for i in range(l-2,-1,-1):
lst[i+1]=lst[i]
lst[0]=temp
print(‘n1‘)
print(lst)
return lst
if __name__==‘__main__‘:
  lst=[]
  #lst=[1,2,3,4,5,6,7,8,9,10]
  for i in range(1,35):
  lst.append(i)
  print(lst)
  #while len(lst)>2:
  print(one_px(lst)[1])
运行结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34]
[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34]
n1
[34, 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32]
[34, 1, 4, 5, 8, 10, 13, 14, 17, 19, 22, 23, 26, 28, 31, 32]
n2
[31, 32, 34, 1, 4, 5, 8, 10, 13, 14, 17, 19, 22, 23, 26, 28]
[31, 32, 1, 4, 8, 10, 14, 17, 22, 23, 28]
n1
[28, 31, 32, 1, 4, 8, 10, 14, 17, 22, 23]
[28, 31, 1, 4, 10, 14, 22, 23]
n2
[22, 23, 28, 31, 1, 4, 10, 14]
[22, 23, 31, 1, 10, 14]
n2
[10, 14, 22, 23, 31, 1]
n
[10, 14, 23, 31]
[10, 14, 31]
n1
[31, 10, 14]
n
[31, 10]
[31, 10]
10

第二种解法:
#在成为领导者之前,成功的全部就是自我成长
#有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。

if __name__==‘__main__‘:

n=10
lst=[]
for i in range(n):
lst.append(i+1)
print(lst)
i=0
k=0
m=0
while(m<n-1):
if(lst[i]!=0):k+=1
if(k==3):
lst[i]=0
k=0
m+=1
i+=1
#print(i)
if(i==n):i=0
print(lst)
运行结果:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 0, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 0, 4, 5, 0, 7, 8, 9, 10]
[1, 2, 0, 4, 5, 0, 7, 8, 9, 10]
[1, 2, 0, 4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 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 10]
[0, 0, 0, 4, 0, 0, 0, 0, 0, 0]













以上是关于记录一次耗时比较久的2种方法的主要内容,如果未能解决你的问题,请参考以下文章

记一次socket_create()函数耗时异常记录

Hexagon DSP系列记录——plain C/intrinsics/asm汇编代码实现耗时比较

Hexagon DSP系列记录——plain C/intrinsics/asm汇编代码实现耗时比较

记录一次耗时编译Gradle一直sync良久问题

使用AOP拦截器获取一次请求流经方法的调用次数和调用耗时

C#记录程序耗时方法