计时功能 [英] Timing functions

查看:29
本文介绍了计时功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告,这有点递归;)

我回答了这个问题:Python:如何在最长元素之前获取列表中的所有元素?

然后我在那里提交了另一个应该更快的答案(作者认为,我也是如此).我试图为不同的解决方案计时,但应该更慢的解决方案实际上更快.这让我觉得我的代码有问题.或者是吗?

导入字符串随机导入导入时间定义解决方案1(lst):返回 lst[:lst.index(max(lst, key=len))]定义解决方案2(lst):idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))返回 lst[:idx]# 创建一个包含 100000 个元素的长列表# 随机数据和随机元素长度lst = []对于我在范围内(100000):s = "".join([random.choice(string.letters+string.digits) for x in range(1, random.randint(1,50))])lst.append(s)# 计时第一个解决方案开始 = time.time()解决方案1(lst)打印解决方案 1 的时间", (time.time() - start)# 计时第二个解决方案开始 = time.time()解决方案2(lst)打印解决方案 2 的时间",(time.time() - 开始)

更新

在有人提到我为什么提出这个问题之前,这是一个新问题.问题更多是关于我学习如何衡量执行时间...

解决方案

Lambda 在第二种解决方案中的成本更高.

我分析了代码和分析数据,看起来,第一个解决方案更快

正如 wiki 所说的那样,函数调用代价高昂,并且在第二种解决方案,lambda 和 len 函数调用使其运行速度变慢

请注意,我已将列表缩减为 1000 个元素

<预><代码>>>>cProfile.run('solution1(lst)')在 0.000 CPU 秒内调用 5 次函数订购者:标准名称ncalls tottime percall cumtime percall filename:lineno(function)1 0.000 0.000 0.000 0.000 <pyshell#305>:1(解决方案 1)1 0.000 0.000 0.000 0.000 <字符串>:1(<模块>)1 0.000 0.000 0.000 0.000 {最大值}1 0.000 0.000 0.000 0.000 {_lsprof.Profiler"对象的禁用"方法}1 0.000 0.000 0.000 0.000 {列表"对象的索引"方法}>>>cProfile.run('solution2(lst)')2004 函数调用时间为 0.012 CPU 秒订购者:标准名称ncalls tottime percall cumtime percall filename:lineno(function)1 0.000 0.000 0.012 0.012 <pyshell#306>:1(解决方案2)1000 0.006 0.000 0.009 0.000 <pyshell#306>:2(<lambda>)1 0.000 0.000 0.012 0.012 <字符串>:1(<模块>)1000 0.003 0.000 0.003 0.000 {len}1 0.003 0.003 0.012 0.012 {最大值}1 0.000 0.000 0.000 0.000 {_lsprof.Profiler"对象的禁用"方法}

Warning, this is a bit recursive ;)

I answered this question: Python:How can i get all the elements in a list before the longest element?

And after I submitted there where another answer that should be faster (the author thought, and so did I). I tried to time the different solutions but the solution that should be slower was actually faster. This made me think there is something wrong with my code. Or is it?

import string
import random
import time

def solution1(lst):
  return lst[:lst.index(max(lst, key=len))]

def solution2(lst):
  idx, maxLenStr = max(enumerate(lst), key=lambda x:len(x[1]))
  return lst[:idx]

# Create a 100000 elements long list that contains
# random data and random element length
lst = []
for i in range(100000):
  s = "".join([random.choice(string.letters+string.digits) for x in range(1, random.randint(1,50))])
  lst.append(s)

# Time the first solution
start = time.time()
solution1(lst)
print 'Time for solution1', (time.time() - start)

# Time the second solution
start = time.time()
solution2(lst)
print 'Time for solution2', (time.time() - start)

Update

Before anyone mentions why I put this is as a new question. The question is more about me learning how to measure execution time...

解决方案

The lambda is costing dearer in the second solution.

I profiled both the codes and by the profile data, it looks, the first solution is faster

As the wiki would say function call is costly and in the second solution, the lambda and the len function calls are making it run slower

Please note, I have trimmed down the list to a length of 1000 elements

>>> cProfile.run('solution1(lst)')
         5 function calls in 0.000 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <pyshell#305>:1(solution1)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
        1    0.000    0.000    0.000    0.000 {method 'index' of 'list' objects}


>>> cProfile.run('solution2(lst)')
         2004 function calls in 0.012 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.012    0.012 <pyshell#306>:1(solution2)
     1000    0.006    0.000    0.009    0.000 <pyshell#306>:2(<lambda>)
        1    0.000    0.000    0.012    0.012 <string>:1(<module>)
     1000    0.003    0.000    0.003    0.000 {len}
        1    0.003    0.003    0.012    0.012 {max}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}

这篇关于计时功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆