Dave Angel, 17.04.2010 10:47:
Alan Gauld wrote:
"Lie Ryan" wrote
A friend of mine suggested me to do the next experiment in python
and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
Are you sure
Alan Gauld wrote:
"Lie Ryan" wrote
A friend of mine suggested me to do the next experiment in python
and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
Are you sure you're not causing Ja
Hi.
Thanks for everyone answers. It's true, hehe, it's not a benchmark
or anything like that. I hadn't taken into account compiler
optimizations, but I have learnt a lot in this thread.
About the Java code, Bigints are used.
ark
___
Tutor maillist -
"Lie Ryan" wrote
A friend of mine suggested me to do the next experiment in python and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
Are you sure you're not causing Java to overflow here?
> result = sum(range(10))
>
> although it still took 10 minutes on my PC.
Did you mean to say "minutes" or rather "seconds" here? And did you
really mean to use "range" or rather "xrange" (or "range" in Py3)?
Yes, minutes and in Python 3.
And on a 2.8GHz 2 core CPU with 2G RAM
su
"Steven D'Aprano" wrote
(We can of course do some fancy math to speed this particular
sum up since the result for any power of ten has a common pattern,
but I wouldn't expect the compiler optimiser to be that clever)
No fancy maths needed,
The sum of 1,2,3,4,...,N is given by a simple for
ALAN GAULD wrote:
The precalculation optimisations are
taking place. If you pass it an argument to use for the upper limit of the
sequence the calculation time shoots up.
I'm still confused about when the addition takes place.
Surely the compiler has to do the addition, so it should
On 04/16/10 16:50, Ark wrote:
> Hi everyone.
> A friend of mine suggested me to do the next experiment in python and Java.
>
> It's a simple program to sum all the numbers from 0 to 10.
>
> result = i = 0
> while i < 10:
> result += i
> i += 1
> print result
>
Are you su
Steven D'Aprano, 16.04.2010 12:00:
On Fri, 16 Apr 2010 06:29:40 pm Alan Gauld wrote:
"Stefan Behnel" wrote
import cython
@cython.locals(result=cython.longlong, i=cython.longlong)
def add():
result = 0
for i in xrange(10):
result += i
On Fri, 16 Apr 2010 06:25:54 pm Stefan Behnel wrote:
> Alan Gauld, 16.04.2010 10:09:
> > Even the built in sum() will be faster than a while loop:
> >
> > result = sum(range(10))
> >
> > although it still took 10 minutes on my PC.
>
> Did you mean to say "minutes" or rather "seconds" here?
On Fri, 16 Apr 2010 06:29:40 pm Alan Gauld wrote:
> "Stefan Behnel" wrote
>
> > import cython
> >
> > @cython.locals(result=cython.longlong, i=cython.longlong)
> > def add():
> > result = 0
> > for i in xrange(10):
> > result += i
> > return
> The precalculation optimisations are
> taking place. If you pass it an argument to use for the upper limit of the
> sequence the calculation time shoots up.
I'm still confused about when the addition takes place.
Surely the compiler has to do the addition, so it should be slower?
I assume
Alan Gauld, 16.04.2010 10:29:
"Stefan Behnel" wrote
import cython
@cython.locals(result=cython.longlong, i=cython.longlong)
def add():
result = 0
for i in xrange(10):
result += i
return result
print add()
This runs in less than half a second on my machine, includin
Dave Angel wrote:
Christian Witts wrote:
Ark wrote:
Hi everyone.
A friend of mine suggested me to do the next experiment in python
and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
The
Alan Gauld wrote:
"Stefan Behnel" wrote
import cython
@cython.locals(result=cython.longlong, i=cython.longlong)
def add():
result = 0
for i in xrange(10):
result += i
return result
print add()
This runs in less than half a second on
Stefan Behnel, 16.04.2010 09:38:
A compiler for a statically compiled language can see that the
above loop yields a constant result, so it can calculate the result in
advance (or at least reduce the loop overhead for the calculation)
instead of generating code for the loop as it stands.
That re
Christian Witts wrote:
Ark wrote:
Hi everyone.
A friend of mine suggested me to do the next experiment in python and
Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
The time for this calc
"Stefan Behnel" wrote
import cython
@cython.locals(result=cython.longlong, i=cython.longlong)
def add():
result = 0
for i in xrange(10):
result += i
return result
print add()
This runs in less than half a second on my machine, inclu
Alan Gauld, 16.04.2010 10:09:
Even the built in sum() will be faster than a while loop:
result = sum(range(10))
although it still took 10 minutes on my PC.
Did you mean to say "minutes" or rather "seconds" here? And did you really
mean to use "range" or rather "xrange" (or "range" in
"Ark" wrote
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
The time for this calculations was huge. It took a long time to give
the result. But, the corresponding program in Java takes less than
Ark wrote:
Hi everyone.
A friend of mine suggested me to do the next experiment in python and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
The time for this calculations was huge. It took
Ark, 16.04.2010 08:50:
A friend of mine suggested me to do the next experiment in python and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i< 10:
result += i
i += 1
print result
I hope you are aware that this is a) a very lous
Hi everyone.
A friend of mine suggested me to do the next experiment in python and Java.
It's a simple program to sum all the numbers from 0 to 10.
result = i = 0
while i < 10:
result += i
i += 1
print result
The time for this calculations was huge. It took a long time t
23 matches
Mail list logo