Re: [Tutor] Loop comparison

2010-04-17 Thread Stefan Behnel
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

Re: [Tutor] Loop comparison

2010-04-17 Thread Dave Angel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Ark
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 -

Re: [Tutor] Loop comparison

2010-04-16 Thread Alan Gauld
"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?

Re: [Tutor] Loop comparison

2010-04-16 Thread Alan Gauld
> 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

Re: [Tutor] Loop comparison

2010-04-16 Thread Alan Gauld
"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

Re: [Tutor] Loop comparison

2010-04-16 Thread Dave Angel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Lie Ryan
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Steven D'Aprano
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?

Re: [Tutor] Loop comparison

2010-04-16 Thread Steven D'Aprano
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

Re: [Tutor] Loop comparison

2010-04-16 Thread ALAN GAULD
> 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

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Christian Witts
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Christian Witts
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Dave Angel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Alan Gauld
"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

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Alan Gauld
"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

Re: [Tutor] Loop comparison

2010-04-16 Thread Christian Witts
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

Re: [Tutor] Loop comparison

2010-04-16 Thread Stefan Behnel
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

[Tutor] Loop comparison

2010-04-15 Thread Ark
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