Re: code optimization (calc PI) / New Algorithme for PI

2007-01-05 Thread Nick Craig-Wood
Michael M. <[EMAIL PROTECTED]> wrote: > Yes, this "gmpy" sounds good for calc things like that. > But not available on my machine. > ImportError: No module named gmpy Sorry, I should have said - you'll need to download that from http://gmpy.sourceforge.net/ > Anyway, thanks for posting. Th

Re: code optimization (calc PI) / New Algorithme for PI

2007-01-04 Thread Michael M.
[EMAIL PROTECTED] wrote: >>Yes, this "gmpy" sounds good for calc things like that. >>But not available on my machine. >>ImportError: No module named gmpy > > > What type of machine? Windoof with Cygwin. > > The home page for gmpy is http://sourceforge.net/projects/gmpy/ > > I have Windows ve

Re: code optimization (calc PI) / New Algorithme for PI

2007-01-04 Thread casevh
> Yes, this "gmpy" sounds good for calc things like that. > But not available on my machine. > ImportError: No module named gmpy What type of machine? The home page for gmpy is http://sourceforge.net/projects/gmpy/ I have Windows versions available at http://home.comcast.net/~casevh/ casevh -

Re: code optimization (calc PI) / New Algorithme for PI

2007-01-04 Thread Michael M.
Mainly, it was fload-div. Changed to int-div (python //) runs faster. Yes, this "gmpy" sounds good for calc things like that. But not available on my machine. ImportError: No module named gmpy Anyway, thanks for posting. This gmpy module can be very intersting. But right now, the focus was, if i

Re: code optimization (calc PI)

2007-01-04 Thread Nick Craig-Wood
mm <[EMAIL PROTECTED]> wrote: > (Yes, I konw whats an object is...) > BTW. I did a translation of a pi callculation programm in C to Python. > (Do it by your own... ;-) > Calc PI for 800 digs(?). (german: Stellen) > int a=1,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5; > for(;

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-04 Thread casevh
> Here is my attempt to convert the C code, not written with speed in mind > and I was too lazy too time it. :-) > > from itertools import izip > > def pi(): > result = list() > d = 0 > e = 0 > f = [2000] * 2801 > for c in xrange(2800, 0, -14): > for b, g in izip(xrang

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread Marc 'BlackJack' Rintsch
In <[EMAIL PROTECTED]>, Michael M. wrote: > * The C is very fast, Python not. > * Target: Do optimization, that Python runs nearly like C. As someone else already asked: Why? You can't beat a compiled to machine code language with an interpreted one when doing integer arithmetic. > counter=c >

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread casevh
Michael M. wrote: > Ok, here is the code. It is a translation of the following code, found > on the internet. > > * The C is very fast, Python not. > * Target: Do optimization, that Python runs nearly like C. There is an error in the translated code. It returns 1600 digits instead of 800 digits.

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread Steven D'Aprano
On Thu, 04 Jan 2007 02:10:44 +0100, Michael M. wrote: > print "\nTiming a 1 million loop 'for loop' ..." > start = time.clock() > for x in range(100): >y = x # do something Why not "pass # do nothing"? > end = time.clock() > print "Time elapsed = ", end - start, "seconds" Are you awar

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread bearophileHUGS
Michael M.: > * The C is very fast, Python not. > * Target: Do optimization, that Python runs nearly like C. Python can't be fast as C for that kind of programs. Note that your original C program gives less digits than the Python program. Your original takes about ~15.2 s on my PC. The following v

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread Gabriel Genellina
At Wednesday 3/1/2007 22:10, Michael M. wrote: Ok, here is the code. It is a translation of the following code, found on the internet. * The C is very fast, Python not. * Target: Do optimization, that Python runs nearly like C. Why? Python is strong in other aspects, *not* on computation spee

Re: code optimization (calc PI) / Full Code of PI calc in Python and C.

2007-01-03 Thread Michael M.
Ok, here is the code. It is a translation of the following code, found on the internet. * The C is very fast, Python not. * Target: Do optimization, that Python runs nearly like C. Auf 800 Stellen in 160 Zeichen... -- int a=1,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5; for

Re: code optimization (calc PI)

2007-01-03 Thread Matimus
> If someone is really interested in speed optimization, I can publish my > PI-calc code. I wouldn't mind seeing it. Chances are you will get much better help if you post your code anyway. -Matt -- http://mail.python.org/mailman/listinfo/python-list

Re: code optimization (calc PI)

2007-01-03 Thread Gabriel Genellina
At Wednesday 3/1/2007 12:50, mm wrote: Hmm... it's a question. It was not that easy to translate this [EMAIL PROTECTED] C-Program into readable code and then to Python. But it works. Because that code was written with C in mind, and uses some C subtlecies (uhm, got it right?) obscuring the or

Re: code optimization (calc PI)

2007-01-03 Thread Michael
Yes. But it still runns very slowly. If someone is really interested in speed optimization, I can publish my PI-calc code. Maybe for some Python compiler/interpreter hackers... ;-) (There are only 2 while-loops, one within another, and some simple basic calculations. Nothing special.) Stefan

Re: code optimization (calc PI)

2007-01-03 Thread Michael
Hmm.. thanks. I did this changes, but without any performance profits. Matimus wrote: > Using the '+' operator for string concatonation can be slow, especially > when done many times in a loop. > > >> pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont > > > The accepted

Re: code optimization (calc PI)

2007-01-03 Thread Stefan Schwarzer
On 2007-01-03 16:50, mm wrote: > More general, maybe there is a speed optimazation docu out there. At least Alex Martellis "Python in a Nutshell" has a section on optimization. I presented this at the last EuroPython conference: http://sschwarzer.com/download/optimization_europython2006.pdf Stef

Re: code optimization (calc PI)

2007-01-03 Thread Matimus
Using the '+' operator for string concatonation can be slow, especially when done many times in a loop. >pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont The accepted solution would be to make pi an array and append to the end... pi = [] #create the array (empty) ... ...

Re: code optimization (calc PI)

2007-01-03 Thread Michael
Ah, no. It was a HASH (assoziative Array or somethings like that). mm wrote: > > I konw, that for example while-loops in Perl are very slow. Maybe this > is also known in Pyhton. Then, I can translate the while-loops in to > for-loops, for example. > More general, maybe there is a speed optim

Re: code optimization (calc PI)

2007-01-03 Thread mm
Hmm... it's a question. It was not that easy to translate this [EMAIL PROTECTED] C-Program into readable code and then to Python. But it works. There are only two while-loops (a while within an other while). I konw, that for example while-loops in Perl are very slow. Maybe this is also known

Re: code optimization (calc PI)

2007-01-03 Thread Diez B. Roggisch
mm wrote: > (Yes, I konw whats an object is...) > BTW. I did a translation of a pi callculation programm in C to Python. > (Do it by your own... ;-) Is that a question on how to optimize code you won't show us? If yes, I'm sorry to tell you that crystal balls are short these days. Too much new-ye

code optimization (calc PI)

2007-01-03 Thread mm
(Yes, I konw whats an object is...) BTW. I did a translation of a pi callculation programm in C to Python. (Do it by your own... ;-) Calc PI for 800 digs(?). (german: Stellen) -- int a=1,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5; for(;d=0,g=c*2;c-=14,printf("%.4d",e+