Without actually reading the assembly/bytecode/etc, you end up speculating about silly things like 'the two evaluations and assignments can happen in parallel, and so may happen on different cores.'.
But you are right: even when you isolate where the code is slow, you've still got a lot of work to do to find out why it is slow.
Added in edit: The magic term is "execution unit" not "core". As I say, things have changed, and the bundling of multiple execution units into each core, and multiple cores into each processor, is different in interesting and subtle ways from the situation I used to code, where I had a few hundred, or a few thousand, processors in each machine, but the individual processors were simpler.
The problem is that the abstraction layer between the python code in question and the processor's instruction stream is so thick that it's hard to say one way or the other that the processor is indeed executing that particular pair of instructions in parallel. It's definitely executing many instructions out of order, but it's unclear (without inspection of the python interpreter and its assembly) what's happening at the machine level.
Looking at the bytecode of the python program at least begins to tells us that the python bytecode of the two versions is fundamentally different, which could account for the performance difference. Although, what exactly makes the material difference is also under debate elsewhere in the thread. :)
I'm mentioning obvious because sometimes it really is, like doing a linear search in a hotspot where it should be a binary tree and things like that.
Looking at assembly/byte code is interesting to understand what you could tweak, but again you need measurements to verify.
Here's INPLACE_ADD:
https://github.com/python/cpython/blob/874809ea389e6434787e7...
And here's BINARY_ADD:
https://github.com/python/cpython/blob/874809ea389e6434787e7...
They're almost identical :)
Line 1296 is PyNumber_Add(left, right) Line 1495 is PyNumber_InPlaceAdd(left, right)
Those go to:
PyNumber_Add https://github.com/python/cpython/blob/874809ea389e6434787e7...
PyNumber_InPlaceAdd https://github.com/python/cpython/blob/874809ea389e6434787e7...
And those are different, though I can't tell how it should matter.
It would not surprise me if the culprit was instead:
98 STORE_FAST 2 (a2)
100 LOAD_FAST 3 (c)
Due to them being the same amalgamated memory location in practice, with the load hitting the in-flight store. But that's more typical of a POWER performance characteristic than an Intel one...Each CPython VM stack frame has three tuples associated with it:
* co_consts is the constant/literal names local to the frame
* co_varnames is all other names local to the frame
* co_names is the all nonlocal names referenced in the frame
Under the hood, Python creates a structure called 'fastlocals' which is basically an array of (pointer to) PyObject, where the PyObject at each index of the array is the value corresponding to the name at the same index of co_varnames. LOAD_FAST and STORE_FAST are extremely simple opcodes; they do a tiny bit of error checking, refcount management (increment on load, decrement on store), and then just manipulate 'fastlocals'.
So, for example, 'LOAD_FAST i' consists in its entirety of:
1. Grab the PyObject at fastlocals[i]
2. If it's null, throw an UnboundLocalError
3. Otherwise, increment its refcount and push it to the top of the frame's evaluation stack.
concurrent: multiple tasks at the same time are "in progress"
parallel: multiple tasks are progressing at the same time
I strongly believe that the idea that it's running on multiple cores is not the case. Python doesn't implicitly schedule any user code on separate threads, and if it did, the coherence cost of doing so would swamp any benefit of doing two simple operations in parallel.
Disassembly is as simple as:
Edit: Whoops, I see that I used Python3, while the post was about 2.7.4, and there's some wrangling over the differences below. I reran the disassmbly under 2.7.4. The bytecode was more verbose, but differed in the same way.