Why timeit doesn't work on my code snippet?
Asked Answered
C

1

6

I think these 3 are logically equivalent, returning the set {1, 3, 4}:

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

But when I try to check the performance of each in ipython (v1.2.1 on python 3.4.0), the timeit magic fails.

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'

What's going on here? Also fails in 2.7. I can't reproduce this using the vanilla python timeit.timeit method.

Chicoine answered 4/6, 2014 at 17:53 Comment(0)
G
4

This looks to me a bug in IPython.

First the workaround

escape the braces, so that the call looks like

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

Now the Problem

If you see the call stack, before the call cascaded down to timeit.py, it passes through

/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

Now if you refer this source, you can see, that before the arguments get passed to the timeit function, its formatted to expand the Python variables in a string

        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 calls DollarFormatter() as the for-matter function whose doc-string states something along the following lines

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. 

So, that is the reason, a set is interpreted as a standard {foo} replacement and gets converted to either a tuple (if comma separated values) or a constant which makes the expression to

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

which is off-course invalid.

Grundyism answered 4/6, 2014 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.