Hi everyone,
While looking over the PyLong source code in Objects/longobject.c I came
across the fact that the PyLong object doesnt't include implementation for
basic inplace operations such as adding or multiplication:
[...]
long_long, /*nb_int*/
0,
>On my machine, the more realistic code, with an implicit C loop,
>the_value = sum(the_increment for i in range(total_iters))
>gives the same value twice as fast as your explicit Python loop.
>(I cut total_iters down to 10**7).
Your code is faster due to a number of reasons:
- range in Python
> My question is -- how can the interpreter know if it can alter what is
> supposed to be an immutable in-place? If it's used only internally to a
> function, the it would be safe, but how to know that?
> -CHB
You can just check the reference count of your object, it's a member of the
PyObject