为什么timeit在我的代码片段上不起作用?

6 投票
1 回答
576 浏览
提问于 2025-04-18 08:36

我觉得这三个写法在逻辑上是等价的,都会返回这个集合 {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(版本1.2.1,使用python 3.4.0)中测试它们的性能时,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版本中也失败了。我用普通的python timeit.timeit方法测试时却没有问题。

1 个回答

4

我觉得这看起来像是IPython中的一个bug。

首先是解决方法

你可以对大括号进行转义,这样调用看起来像是

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()作为格式化函数,它的文档字符串大致说明了以下内容

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())

这当然是无效的。

撰写回答