Why is b.pop(0) over 200 times slower than del b[0] for bytearray?

When you run b.pop(0), Python moves all the elements back by one as you might expect. This takes O(n) time. When you del b[0], Python simply increases the start pointer of the object by 1. In both cases, PyByteArray_Resize is called to adjust the size. When the new size is smaller than half the allocated … Read more

Why is string’s startswith slower than in?

As already mentioned in the comments, if you use s.__contains__(“XYZ”) you get a result that is more similar to s.startswith(“XYZ”) because it needs to take the same route: Member lookup on the string object, followed by a function call. This is usually somewhat expensive (not enough that you should worry about of course). On the … Read more

deque.popleft() and list.pop(0). Is there performance difference?

deque.popleft() is faster than list.pop(0), because the deque has been optimized to do popleft() approximately in O(1), while list.pop(0) takes O(n) (see deque objects). Comments and code in _collectionsmodule.c for deque and listobject.c for list provide implementation insights to explain the performance differences. Namely that a deque object “is composed of a doubly-linked list”, which … Read more

How exactly is Python Bytecode Run in CPython?

Yes, your understanding is correct. There is basically (very basically) a giant switch statement inside the CPython interpreter that says “if the current opcode is so and so, do this and that”. http://hg.python.org/cpython/file/3.3/Python/ceval.c#l790 Other implementations, like Pypy, have JIT compilation, i.e. they translate Python to machine codes on the fly.

Why is code using intermediate variables faster than code without?

My results were similar to yours: the code that uses intermediate variables was consistently at least 10-20 % faster in Python 3.4. 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 … Read more

list() uses slightly more memory than list comprehension

I think you’re seeing over-allocation patterns this is a sample from the source: /* This over-allocates proportional to the list size, making room * for additional growth. The over-allocation is mild, but is * enough to give linear-time amortized behavior over a long * sequence of appends() in the presence of a poorly-performing * system … Read more

What is python-dev package used for

python-dev python-dev contains the header files you need to build Python extensions. lxml lxml is a Python C-API extension that is compiled when you do pip install lxml. The lxml sources have at least something like #include <Python.h> in the code. The compiler looks for the header file Python.h during compilation, hence those header files … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)