Re:Pycraft: Minecraft engine in Python
by squeaky-clean on Tuesday April 19, 2016 @10:42AM
(#11527308)
That doesn't tell you about the speed of the code though.
$> python -m timeit "1 <= 4 <= 5"
10000000 loops, best of 3: 0.0877 usec per loop
$> python -m timeit "4 in range(1,5)"
1000000 loops, best of 3: 0.413 usec per loop
$> python -m timeit "4 in (1,2,3,4,5)"
10000000 loops, best of 3: 0.0776 usec per loop
Tested in python 3.5.0, Even with the improvements to the range function, the comparison is almost 5x faster.Eh? That dis output doesn't really tell us which is more efficient. To know that, you'd need to know how `COMPARE_OP` works for each of it's arguments, which is what @Negative1 believes will be not-as-efficient (along with range creation).
This output doesn't really provide any clarification.
It may be a shorter number of bytecodes, but that does not mean it has better performance.
You have two function calls in the range version and only one in the non-range version. You are just hiding some of that code in a dynamic function call. Also, your compare_op consists of two <= in the non-range version and an "in" compare_op for the range version (which probably hides the element by element comparison or hash/bloom if it is O(1).
Fewer lines of code does not mean more efficient. Regardless of whether it is source code or bytecode.
Re:Pycraft: Minecraft engine in Python
by maxerickson on Tuesday April 19, 2016 @08:24AM
(#11526425)
There's no argument made in the comment you replied to. Certainly not a strident one in favor of range.
It appears to me they went ahead and posted the bytecode for both because they had spun up an interpreter to get the bytecode for one.
Re:Pycraft: Minecraft engine in Python
by lifthrasiir on Tuesday April 19, 2016 @03:37AM
(#11525433)
The bytecode count is, while significant in slower CPython interpreters, not directly an indicator of performance. `LOAD_GLOBAL` and `CALL_FUNCTION` are comparatively expensive opcodes to run.
You can actually get a very good idea for what Python is doing under the hood using the dis module:
Compare to: