Lorenz Quack schrieb:
Gregor Lingl wrote:
...
pression without the in-place operator (as would be necessary if we
go with immutable types).
Hi Lorenz,
I'd propose not to confuse two things:
(1) use of syntax x = x + x versus x += x
(2) use of immutable versus mutable datatypes
In python you can write
>>> a = 2
>>> a = a + a
>>> a
4
as well as
>>> a += a
>>> a
8
although the int type is immutable.
true, but as a performance test I think my approach is still valid
because if you do
ok. my objection concerned also some previous postings in this thread
that stated,
that use of a+=b makes a mutable vector type necessary.
>>> 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]
Did you use two different Vector2d classes here, one mutable and one
immutable? Why do they
have the same name then? Or did you merely implement the operations x+=x
and x=x+x differently?
If x = x + y creates a new object x or changes x is also a matter of how
it is implemented.
Moreover it is my conviction that one must not decide about which
data type to use on
the basis of a +- 50 percent difference in performance.
One more remark: At least on module of the standard library of Python
has a (rather simple)
2d-Vector class implemented in pure Python, which of course has a
considerably worse performance,
by a factor of 4 approximately:
>>> timeit.repeat("x = Vec2D(2,3); x = x + x", "from turtle import
Vec2D", repeat=1, number=10000000)
[25.274672320512536]
Nevertheless one would expect a class implemented in C to run *much*
faster than a pure Python solution.
So I suspect that your implementation may not be sufficiently
significant to serve as a criterion to
decide that issue.
Best regards,
Gregor
.
so the numbers change but the overall result stays the same.