Python timeit不适用于list.remove操作 [英] Python timeit doesn't works for list.remove operation

查看:115
本文介绍了Python timeit不适用于list.remove操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过timeit模块检查python列表中remove操作的性能,但是会引发ValueError.

I was trying to check the performance of remove operation in python list through the timeit module, but it throws a ValueError.

In [4]: a = [1, 2, 3]

In [5]: timeit a.remove(2)

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-7b32a87ebb7a> in <module>()
----> 1 get_ipython().magic('timeit a.remove(2)')

/home/amit/anaconda3/lib/python3.4/site-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2334         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2335         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2336         return self.run_line_magic(magic_name, magic_arg_s)
   2337 
   2338     #-------------------------------------------------------------------------

..
..
..
..

ValueError: list.remove(x): x not in list

推荐答案

每次在第一次循环后删除2时,您都需要创建列表:

You need to create the list each time as you have removed the 2 after the first loop:

In [2]: %%timeit
   ...: a = [1,2,3]
   ...: a.remove(2)
   ...: 

10000000 loops, best of 3: 159 ns per loop

您可以看到存在10000000个循环,因此通过在运行时间之外创建列表,可以删除2第一个循环,因此在以后的运行中没有要删除的2个循环.

You can see there were 10000000 loops so by creating the list outside the timeit run you removed 2 the first loop so there is no 2 to remove on subsequent runs.

您可以对列表的创建时间进行计时,并减去该时间以近似于删除列表所花费的时间.

You can time the list creation and subtract that to get an approximation of the time it takes to just remove.

In [3]: timeit a =  [1,2,3]
10000000 loops, best of 3: 89.8 ns per loop

您可以运行一次,但是您的时间安排将不那么准确:

You could run it once but your timings won't be near as accurate:

In [12]: a = [1,2,3]

In [13]: timeit -n 1 -r 1 a.remove(2)
1 loops, best of 1: 4.05 µs per loop

timeit magic命令的选项位于文档:

The options for timeit magic command are in the docs:

选项:

-n<N>:在循环中执行给定语句<N>次.如果未指定该值,则选择一个合适的值.

-n<N>: execute the given statement <N> times in a loop. If this value is not given, a fitting value is chosen.

-r<R>:重复循环迭代<R>次,并获得最佳结果.默认值:3

-r<R>: repeat the loop iteration <R> times and take the best result. Default: 3

-t:使用time.time来测量时间,这是Unix上的默认值.此功能可测量墙壁时间.

-t: use time.time to measure the time, which is the default on Unix. This function measures wall time.

-c:使用time.clock来测量时间,这是Windows上的默认设置,用于测量墙壁时间.在Unix上,改用resource.getrusage并返回CPU用户时间.

-c: use time.clock to measure the time, which is the default on Windows and measures wall time. On Unix, resource.getrusage is used instead and returns the CPU user time.

-p<P>:使用<P>位的精度显示计时结果.默认值:3

-p<P>: use a precision of <P> digits to display the timing result. Default: 3

-q:安静,不打印结果.

-q: Quiet, do not print result.

-o:返回一个TimeitResult,可以将其存储在变量中以进行检查 结果的更多细节.

-o: return a TimeitResult that can be stored in a variable to inspect the result in more details.

这篇关于Python timeit不适用于list.remove操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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