>>> a = 2
>>> a += a
I believe the interpreter internally takes the two operands (in this
case a and a) adds them and then rebinds the result to a (the id
changes) so effectively doing
>>> a = a + a
exactly because a is immutable
if a were mutable the two expressions would be indeed different the +=
version would not create a new instance and rebind the name a to it
but modify the object a is referring to, while a = a + a would again
create a new object and rebind it.
So therefore I believe that this test does make sense.
Tell me if I'm wrong somewhere.
here are the calls with the results:
[snip]
which has more than 30000 digits. Which result did you get after
10000000 executions of the statement x = x + x?
And which implementation of the long integer type did you use that is
that much faster than Python's ?
Regards,
Gregor
indeed those are valid objections. well first of all I used a
self-written C extension with double as the underlying type. but the
result after 1023 iterations turns into (inf, inf). this could of
course invalidate the results so I modified the test:
>>> timeit.repeat("x = Vector2d(2,3); x += x", "from vector import
Vector2d", repeat=5, number=10000000)
[5.1832518577575684,
5.1106431484222412,
5.1510121822357178,
5.0923140048980713,
5.0608019828796387]
>>> timeit.repeat("x = Vector2d(2,3); x = x + x", "from vector import
Vector2d", repeat=5, number=10000000)
[6.5348029136657715,
6.3499071598052979,
6.4433431625366211,
6.412431001663208,
6.4398849010467529]
>>> timeit.repeat("x = Vector2d(2,3)", "from vector import Vector2d",
repeat=5, number=10000000)
[3.7264928817749023,
3.6346859931945801,
3.6241021156311035,
3.7733709812164307,
3.6264529228210449]