Why is code using intermediate variables faster than code without?
Why is code using intermediate variables faster than code without?
I have encountered this weird behavior and failed to explain it. These are the benchmarks:
py -3 -m timeit "tuple(range(2000)) == tuple(range(2000))"
10000 loops, best of 3: 97.7 usec per loop
py -3 -m timeit "a = tuple(range(2000)); b = tuple(range(2000)); a==b"
10000 loops, best of 3: 70.7 usec per loop
How come comparison with variable assignment is faster than using a one liner with temporary variables by more than 27%?
By the Python docs, garbage collection is disabled during timeit so it can't be that. Is it some sort of an optimization?
The results may also be reproduced in Python 2.x though to lesser extent.
Running Windows 7, CPython 3.5.1, Intel i7 3.40 GHz, 64 bit both OS and Python. Seems like a different machine I've tried running at Intel i7 3.60 GHz with Python 3.5.0 does not reproduce the results.
Running using the same Python process with timeit.timeit() @ 10000 loops produced 0.703 and 0.804 respectively. Still shows although to lesser extent. (~12.5%)
timeit.timeit()
dis.dis("tuple(range(2000)) == tuple(range(2000))")
dis.dis("a = tuple(range(2000)); b = tuple(range(2000)); a==b")
If you attempt to reproduce this, please run the test multiple times in different execution orders. – Regardless of the result and the odd-ness of this, I think the question is not particularly valuable for SO.
– poke
Apr 11 '16 at 12:49
I think this is pretty interesting. @poke you need to remember that an answer to a similar phenomenon is now the most upvoted answer in stackoverflow.
– Antti Haapala
Apr 11 '16 at 12:50
Also, please try to run the test in a single Python process using the
timeit module directly. Comparisons between two separate Python processes might be affected by the operating system’s task scheduler or other effects.– poke
Apr 11 '16 at 12:52
timeit
@aluriak "best of 3" means best of three averages. This is done because some average might include, say, an unexpected process stall. Taking the best of averages avoids that.
– Veedrac
Apr 16 '16 at 18:23
2 Answers
2
My results were similar to yours: the code using intermediate variables was pretty consistently at least 10-20 % faster in the Python 3.4 that I tired. However when I used IPython on the very same Python 3.4 interpreter, I got these results:
In [1]: %timeit -n10000 -r20 tuple(range(2000)) == tuple(range(2000))
10000 loops, best of 20: 74.2 µs per loop
In [2]: %timeit -n10000 -r20 a = tuple(range(2000)); b = tuple(range(2000)); a==b
10000 loops, best of 20: 75.7 µs per loop
Notably, I never managed to get even close to the 74.2 µs for the former when I used -mtimeit from the command line.
-mtimeit
So this Heisenbug turned out to be something quite interesting. I decided to run the command with strace and indeed there is something fishy going on:
strace
% strace -o withoutvars python3 -m timeit "tuple(range(2000)) == tuple(range(2000))"
10000 loops, best of 3: 134 usec per loop
% strace -o withvars python3 -mtimeit "a = tuple(range(2000)); b = tuple(range(2000)); a==b"
10000 loops, best of 3: 75.8 usec per loop
% grep mmap withvars|wc -l
46
% grep mmap withoutvars|wc -l
41149
Now that is a good reason for the difference. The code that does not use variables causes the mmap system call be called almost 1000x more than the one that uses intermediate variables.
mmap
The withoutvars is full of mmap/munmap for a 256k region; these same lines are repeated over and over again:
withoutvars
mmap
munmap
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000
munmap(0x7f32e56de000, 262144) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000
munmap(0x7f32e56de000, 262144) = 0
mmap(NULL, 262144, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f32e56de000
munmap(0x7f32e56de000, 262144) = 0
The mmap call seems to be coming from the function _PyObject_ArenaMmap from Objects/obmalloc.c; the obmalloc.c also contains the macro ARENA_SIZE, which is #defined to be (256 << 10) (that is 262144); similarly the munmap matches the _PyObject_ArenaMunmap from obmalloc.c.
mmap
_PyObject_ArenaMmap
Objects/obmalloc.c
obmalloc.c
ARENA_SIZE
#define
(256 << 10)
262144
munmap
_PyObject_ArenaMunmap
obmalloc.c
obmalloc.c says that
obmalloc.c
Prior to Python 2.5, arenas were never free()'ed. Starting with Python 2.5,
we do try to free() arenas, and use some mild heuristic strategies to increase
the likelihood that arenas eventually can be freed.
free()
free()
Thus these heuristics and the fact that Python object allocator releases these free arenas as soon as they're emptied lead to python3 -mtimeit 'tuple(range(2000)) == tuple(range(2000))' triggering pathological behaviour where one 256 kiB memory area is re-allocated and released repeatedly; and this allocation happens with mmap/munmap, which is comparatively costly as they're system calls - furthermore, mmap with MAP_ANONYMOUS requires that the newly mapped pages must be zeroed - even though Python wouldn't care.
python3 -mtimeit 'tuple(range(2000)) == tuple(range(2000))'
mmap
munmap
mmap
MAP_ANONYMOUS
The behaviour is not present in the code that uses intermediate variables, because it is using slightly more memory and no memory arena can be freed as some objects are still allocated in it. That is because timeit will make it into a loop not unlike
timeit
for n in range(10000)
a = tuple(range(2000))
b = tuple(range(2000))
a == b
Now the behaviour is that both a and b will stay bound until they're *reassigned, so in the second iteration, tuple(range(2000)) will allocate a 3rd tuple, and the assignment a = tuple(...) will decrease the reference count of the old tuple, causing it to be released, and increase the reference count of the new tuple; then the same happens to b. Therefore after the first iteration there are always at least 2 of these tuples, if not 3, so the thrashing doesn't occur.
a
b
tuple(range(2000))
a = tuple(...)
b
Most notably it cannot be guaranteed that the code using intermediate variables is always faster - indeed in some setups it might be that using intermediate variables will result in extra mmap calls, whereas the code that compares return values directly might be fine.
mmap
Someone asked that why this happens, when timeit disables garbage collection. It is indeed true that timeit does it:
timeit
timeit
Note
By default, timeit() temporarily turns off garbage collection during the timing. The advantage of this approach is that it makes independent timings more comparable. This disadvantage is that GC may be an important component of the performance of the function being measured. If so, GC can be re-enabled as the first statement in the setup string. For example:
timeit()
However, the garbage collector of Python is only there to reclaim cyclic garbage, i.e. collections of objects whose references form cycles. It is not the case here; instead these objects are freed immediately when the reference count drops to zero.
Woha, that's interesting. Shouldn't the garbage collector (which is disabled on timeit) take care of freeing or at least should take care of it? And it raises another question: Aren't those repeated calls a bug?
– Bharel
Apr 11 '16 at 14:55
@Bharel more like "broken as designed"
– Antti Haapala
Apr 11 '16 at 14:56
last but not least, how would this explain the difference between systems, even with the same OS?
– Bharel
Apr 11 '16 at 15:24
@Bharel It depends whether or not a new memory arena is allocated; it is quite possible that other systems have partially free arenas that have enough free memory in the pools that more is not required. Even the same Python version on superficially similar systems might have differing behaviour - things like Python installation path, number of packages in
site-packages, environment variables, current working directory - they all affect the memory layout of the process.– Antti Haapala
Apr 11 '16 at 15:32
site-packages
@Bharel: The garbage collector in CPython is more properly called the "cyclic garbage collector"; it's solely concerned with freeing isolated reference cycles, not general garbage collection. All other cleanup is synchronous and in-order; if the last reference to the last object in an arena is released, the object is immediately deleted, and the arena immediately freed, no cyclic garbage collector involvement required. That's why it's legal to disable
gc; if it disabled general cleanup, you'd run out of memory pretty darn quick.– ShadowRanger
Apr 11 '16 at 20:34
gc
The first question here has to be, is it reproducable? For some of us at least it definitely is though other people say they aren't seeing the effect.
This on Fedora, with the equality test changed to is as actually doing a comparison seems irrelevant to the result, and the range pushed up to 200,000 as that seems to maximise the effect:
is
$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b"
100 loops, best of 3: 7.03 msec per loop
$ python3 -m timeit "a = tuple(range(200000)) is tuple(range(200000))"
100 loops, best of 3: 10.2 msec per loop
$ python3 -m timeit "tuple(range(200000)) is tuple(range(200000))"
100 loops, best of 3: 10.2 msec per loop
$ python3 -m timeit "a = b = tuple(range(200000)) is tuple(range(200000))"
100 loops, best of 3: 9.99 msec per loop
$ python3 -m timeit "a = b = tuple(range(200000)) is tuple(range(200000))"
100 loops, best of 3: 10.2 msec per loop
$ python3 -m timeit "tuple(range(200000)) is tuple(range(200000))"
100 loops, best of 3: 10.1 msec per loop
$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b"
100 loops, best of 3: 7 msec per loop
$ python3 -m timeit "a = tuple(range(200000)); b = tuple(range(200000)); a is b"
100 loops, best of 3: 7.02 msec per loop
I note that variations between the runs, and the order in which the expressions are run make very little difference to the result.
Adding assignments to a and b into the slow version doesn't speed it up. In fact as we might expect assigning to local variables has negligible effect. The only thing that does speed it up is splitting the expression entirely in two. The only difference this should be making is that it reduces the maximum stack depth used by Python while evaluating the expression (from 4 to 3).
a
b
That gives us the clue that the effect is related to stack depth, perhaps the extra level pushes the stack across into another memory page. If so we should see that making other changes that affect the stack will change (most likely kill the effect), and in fact that is what we see:
$ python3 -m timeit -s "def foo():
tuple(range(200000)) is tuple(range(200000))" "foo()"
100 loops, best of 3: 10 msec per loop
$ python3 -m timeit -s "def foo():
tuple(range(200000)) is tuple(range(200000))" "foo()"
100 loops, best of 3: 10 msec per loop
$ python3 -m timeit -s "def foo():
a = tuple(range(200000)); b = tuple(range(200000)); a is b" "foo()"
100 loops, best of 3: 9.97 msec per loop
$ python3 -m timeit -s "def foo():
a = tuple(range(200000)); b = tuple(range(200000)); a is b" "foo()"
100 loops, best of 3: 10 msec per loop
So, I think the effect is entirely due to how much Python stack is consumed during the timing process. It is still weird though.
2 machines with the same memory sticks and same OS cause different results though. Stack depth sounds like a good theory but it does not explain the difference between machines.
– Bharel
Apr 11 '16 at 13:30
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Compare
dis.dis("tuple(range(2000)) == tuple(range(2000))")todis.dis("a = tuple(range(2000)); b = tuple(range(2000)); a==b"). In my configuration second snippet actually contains all of bytecode from first one, and some additional instructions. It's hard to believe that more bytecode instructions results in faster execution. Maybe it's some bug in specific Python version?– Łukasz Rogalski
Apr 11 '16 at 12:42