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

Michael M. michael at mustun.ch
Wed Jan 3 20:10:44 EST 2007


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=10000,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+d/a),e=d%a)for(b=c;d+=f[b]*a,
     f[b]=d%--g,d/=g--,--b;d*=b);}

$ ./a.exe
31415926535897932384626433832795028841971693993751058209749445923078164062862089
98628034825342117067982148086513282306647093844609550582231725359408128481117450
28410270193852110555964462294895493038196442881097566593344612847564823378678316
52712019091456485669234603486104543266482133936072602491412737245870066063155881
74881520920962829254091715364367892590360011330530548820466521384146951941511609
43305727036575959195309218611738193261179310511854807446237996274956735188575272
48912279381830119491298336733624406566430860213949463952247371907021798609437027
70539217176293176752384674818467669405132000568127145263560827785771342757789609
17363717872146844090122495343014654958537105079227968925892354201995611212902196
08640344181598136297747713099605187072113499999983729780499510597317328160963185



Here the Python:
----------------------------------------------------
#!/usr/bin/python
# -*- coding: utf-8 -*-

## http://de.wikipedia.org/wiki/Pi_(Kreiszahl)


from __future__ import division
from array import array
import decimal
import time


#int a=10000,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+d/a),e=d%a)for(b=c;d+=f[b]*a,
#    f[b]=d%--g,d/=g--,--b;d*=b);}
#


print "\nTiming a 1 million loop 'for loop' ..."
start = time.clock()
for x in range(1000000):
   y = x  # do something
end = time.clock()
print "Time elapsed = ", end - start, "seconds"


start_prg = time.clock()


#pi=[]  ## create an empty array
pi=''

a=10000
b=0
c=5600  ## c=2800
d=0
e=0
f=[] # f[2801]
g=0


counter=c
while 0<=counter+4000:
   f.append(2000)  #   f.append( int(a/5) )
   counter=counter-1
   # b=b+1

#print "DEBUG: b counter: ", b, counter


while (c*2):
   d=0

   g=c*2  ## see while condition


   ## ---------------------------
   b=c  ## anzahl elemente
   #print "DEBUG: before 3 while loop..."
   #print "DEBUG: b=", b
   while (b-1):
     d = d + f[b]*a
     g=g-1
     f[b] = d%g
     d = int(d/g)  ## needs cast to int
     g=g-1

     d=d*b

     b=b-1  ## see while condition

     #print "DEBUG: d=", d


   c = c-14;
   #pi.append(str("%04d" % int(e + d/a))) # append to it
   pi = pi + str("%04d" % int(e + d/a))
   #print "%04d" % int(e + d/a),
   ## need cast to int
   #print int(e + int(d/a))
   e = d%a;

#print "".join(pi)
print pi

end_prg = time.clock()
print "Total Time elapsed = ", end_prg - start_prg, "seconds"


## EOF.
-------------------------------



Matimus wrote:

>>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
> 



More information about the Python-list mailing list