python - 为什么 timeit 对我的代码片段不起作用?

标签 python ipython timeit

我认为这 3 个在逻辑上是等价的,返回集合 {1, 3, 4}:

set(sum(((1, 3), (4,), (1,)), ()))
set(sum([[1, 3], [4], [1]], []))
functools.reduce(operator.or_, ({1, 3}, {4}, {1}), set())

但是当我尝试在 ipython(python 3.4.0 上的 v1.2.1)中检查每个的性能时,timeit 魔法失败了。

In [1]: from operator import or_; from functools import reduce

In [2]: timeit set(sum([[1, 3], [4], [1]], []))
1000000 loops, best of 3: 604 ns per loop

In [3]: timeit set(sum(((1, 3), (4,), (1,)), ()))
1000000 loops, best of 3: 330 ns per loop

In [4]: timeit reduce(or_, ({1, 3}, {4}, {1}), set())
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-83628f6293f3> in <module>()
----> 1 get_ipython().magic('timeit reduce(or_, ({1, 3}, {4}, {1}), set())')

/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py in magic(self, arg_s)
   2164         magic_name, _, magic_arg_s = arg_s.partition(' ')
   2165         magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
-> 2166         return self.run_line_magic(magic_name, magic_arg_s)
   2167 
   2168     #-------------------------------------------------------------------------

/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2085                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2086             with self.builtin_trap:
-> 2087                 result = fn(*args,**kwargs)
   2088             return result
   2089 

/usr/lib/python3/dist-packages/IPython/core/magics/execution.py in timeit(self, line, cell)

/usr/lib/python3/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    190     # but it's overkill for just that one bit of state.
    191     def magic_deco(arg):
--> 192         call = lambda f, *a, **k: f(*a, **k)
    193 
    194         if isinstance(arg, collections.Callable):

/usr/lib/python3/dist-packages/IPython/core/magics/execution.py in timeit(self, line, cell)
    929             number = 1
    930             for i in range(1, 10):
--> 931                 if timer.timeit(number) >= 0.2:
    932                     break
    933                 number *= 10

/usr/lib/python3.4/timeit.py in timeit(self, number)
    176         gc.disable()
    177         try:
--> 178             timing = self.inner(it, self.timer)
    179         finally:
    180             if gcold:

<magic-timeit> in inner(_it, _timer)

TypeError: unsupported operand type(s) for |: 'set' and 'tuple'

这是怎么回事?在 2.7 中也失败。我无法使用 vanilla python timeit.timeit 方法重现它。

最佳答案

在我看来,这是 IPython 中的一个错误。

首先是解决方法

转义大括号,这样调用看起来像

timeit reduce(or_, ({{1, 3}}, {{4}}, {{1}}), set())

现在是问题

如果你看到调用堆栈,在调用级联到 timeit.py 之前,它通过了

/usr/lib/python3/dist-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line)
   2085                 kwargs['local_ns'] = sys._getframe(stack_depth).f_locals
   2086             with self.builtin_trap:
-> 2087                 result = fn(*args,**kwargs)
   2088             return result

现在,如果您引用此源,您可以看到,在将参数传递给 timeit 函数之前,它的格式是在字符串中扩展 Python 变量

        magic_arg_s = self.var_expand(line, stack_depth)
        # Put magic args in a list so we can call with f(*a) syntax
        args = [magic_arg_s]

self.var_expand 调用 DollarFormatter() 作为 for-matter 函数,其文档字符串说明了以下几行内容

class DollarFormatter(FullEvalFormatter):
    """Formatter allowing Itpl style $foo replacement, for names and attribute
    access only. Standard {foo} replacement also works, and allows full
    evaluation of its arguments. 

因此,这就是原因,集合被解释为标准的 {foo} 替换,并被转换为元组(如果是逗号分隔值)或使表达式成为

reduce(or_, ((1, 3), 4, 1), set())

这是无效的。

关于python - 为什么 timeit 对我的代码片段不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24044281/

相关文章:

IPython Notebook 以前的单元格内容

python - 时间是 Python。它是如何工作的?

python - timeit 返回什么时间单位?

python - 使用 re 在 python 中匹配数字

python - cv2.imshow() 崩溃内核

python - 在一系列上下文管理器上友好地使用 Python 可迭代对象

python - Matplotlib 和 Ipython 笔记本 : Displaying exactly the figure that will be saved

python - 为什么集合减法和 .difference() 运行速度不同

一行中的python多个 'for'语句

python - 检查列表中每个元素的第一个数字是否相同