473,803 Members | 3,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

code optimization (calc PI)

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=10000,b,c=280 0,d,e,f[2801],g;main(){for(; b-c;)f[b++]=a/5;
for(;d=0,g=c*2; c-=14,printf("%.4 d",e+d/a),e=d%a)for(b= c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}

$ ./a.exe
314159265358979 323846264338327 950288419716939 937510582097494 459230781640628 62089
986280348253421 170679821480865 132823066470938 446095505822317 253594081284811 17450
284102701938521 105559644622948 954930381964428 810975665933446 128475648233786 78316
527120190914564 856692346034861 045432664821339 360726024914127 372458700660631 55881
748815209209628 292540917153643 678925903600113 305305488204665 213841469519415 11609
433057270365759 591953092186117 381932611793105 118548074462379 962749567351885 75272
489122793818301 194912983367336 244065664308602 139494639522473 719070217986094 37027
705392171762931 767523846748184 676694051320005 681271452635608 277857713427577 89609
173637178721468 440901224953430 146549585371050 792279689258923 542019956112129 02196
086403441815981 362977477130996 051870721134999 999837297804995 105973173281609 63185
--------

But Python is much slower here then C here. I used a while-loop within a
while-loop. Not that much calculation here.
Jan 3 '07 #1
21 2601
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-year-outlooks.

Diez
Jan 3 '07 #2
mm

Hmm... it's a question. It was not that easy to translate this #@*?%!
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 in Pyhton. Then, I can translate the while-loops in to
for-loops, for example.
More general, maybe there is a speed optimazation docu out there.

(Anyway, this code for C-calc is complex. And I realy don't understand
it right now... 8-)
But anyway, there is not that much calculation, it has more something to
do with the too while-loops. Here is some code:

(In general, it has basically nothing to do with PI-calc.)
c=2800 ## a counter
while c*2:
## do some calc. did not change c here.
...
b=c ## number of elements

while (b-1):
## so some calc.
...
b=b-1 ## just for while-loop condition.
c = c-14; ## this is code vor the 1st while-loop, BUT bust run after
the 2nd-while-loop.
pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont
know.
There are a output string, a list, and integers. No complex data
structures; and no complex calculations.

Diez B. Roggisch wrote:
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-year-outlooks.

Diez
Jan 3 '07 #3

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 optimazation docu out there.
Jan 3 '07 #4
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)
....
....
pi.append(str(" %04d"%int(e+d/a))) # append to it

And when it is time to print the results do the following:

print "".join(pi)

It may look strange, but it is a common Python idiom. You might also
look into an optimizer such as psycho: http://psyco.sourceforge.net/.

Jan 3 '07 #5
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/optim...python2006.pdf

Stefan

--
Dr.-Ing. Stefan Schwarzer
SSchwarzer.com - Softwareentwick lung für Technik und Wissenschaft
http://sschwarzer.com
Jan 3 '07 #6
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 solution would be to make pi an array and append to the
end...

pi = [] #create the array (empty)
...
...
pi.append(str(" %04d"%int(e+d/a))) # append to it

And when it is time to print the results do the following:

print "".join(pi)

It may look strange, but it is a common Python idiom. You might also
look into an optimizer such as psycho: http://psyco.sourceforge.net/.
Jan 3 '07 #7

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 Schwarzer wrote:
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/optim...python2006.pdf

Stefan
Jan 4 '07 #8
At Wednesday 3/1/2007 12:50, mm wrote:
>Hmm... it's a question. It was not that easy to translate this #@*?%!
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 original algorithm.
BTW, do you know where the algorithm was taken from?
>while c*2:
`while c:` does *exactly* the same without computing (and discarding)
an intermediate object.
while (b-1):
If b>=1 initially (as it should, else this will be a loooooong loop),
this is the same as `while b>1:` but without computing (and
discarding) an intermediate object.
b=b-1 ## just for while-loop condition.
I'm not sure if the compiler is smart enough to translate this into `b -= 1`
c = c-14; ## this is code vor the 1st while-loop, BUT bust run after
Same as above
pi = pi + str("%04d" % int(e + d/a)) ## this should be fast?! I dont
Someone else already pointed out how to improve this.

You don't show the rest of the code...
--
Gabriel Genellina
Softlab SRL


_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 4 '07 #9
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

Jan 4 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1537
by: Astra | last post by:
Hi All Wonder if you can help me with the following calc, which I want to create to check whether or not a user has entered a valid credit card expiry date: 1) I have retrieve a value from 2 x SELECTs. One value is the month in a 2 DIGIT form, eg 01, 02, 03, 04, etc, and the other is the year in a 4 DIGIT form, ie 2004, 2005, 2006, 2007, etc. 2) My Maths for this was going to be that I do a calc on the figures
3
1581
by: enfis.the.paladin | last post by:
Hi to all! I have something like this: class FWrap { public: virtual void READ (void) = 0; } class Optimized { private:
2
2511
by: Mountain Bikn' Guy | last post by:
This code is used in a simple example calculator. It has a text box that displays the result. It performs addition, subtraction, multiplication, etc. on two operands. For the operands it accepts digits 0..9 and a decimal point. The insert() method isn't very clear to me. Can anyone help me convert the code shown below to C#? Thanks! enum { PRECISION = 14 }; //-------------------------------------------------------------------
10
2148
by: Mike | last post by:
Is it still true that the managed C++ compiler will produce much better opimizations than the C# compiler, or have some of the more global/aggressive opimizations been rolled into the 2005 compiler? Are simple common sub-expressions and loop invariants optimized out in the current optimizer? thanks, m
0
3427
by: jdavidson | last post by:
I have not worked with VB at all and wondered if perhaps there was an all knowing SQL DBA out there that also spoke fluent VB. I am trying to come up with the equilivant T-SQL code for the following VB script (it is a portion of an old Access query) and certainly appreciate any help offered!! Thanks (and sorry about the table names....i didn't create the database) 1) & " " & IIf( Is Not Null, & ". ","") & AS , 2)...
4
4849
by: Patrick Sullivan | last post by:
dim s_err as stringbuilder dim xx(6) as double dim ret_flag as integer ' This is a function to call an unmanaged C-style library function. ' The lib function fills an array of 6 doubles or error string. Declare Auto Function calc Lib "calc32.dll" Alias "calc32.dll"( ByRef xx() As Double, ByVal serr As System.Text.StringBuilder) As Integer
11
4923
by: Osiris | last post by:
I have these pieces of C-code (NOT C++ !!) I want to call from Python. I found Boost. I have MS Visual Studio 2005 with C++. is this the idea: I write the following C source file: ============================ #include <iostream> #include <stdafx.h>
4
1351
by: wutzke | last post by:
I started working on a script that would take the cost of something and suggest a retail amount. I usually figure Retail on Margins. So Retail equals Cost divided by (Margin-100)% plus Cost. So if Cost is $50 and Margin is 40% 50/(40-100)%+50=80 or (50/60%)+50=80
7
1449
by: colin | last post by:
Hi, Is there a way of doing simple code generation inside visual c# express such as similar to preprocessing in c++ ? I need to generate a library for some vector maths, but I need to implement it for different types wich would be difficult to do with generic types and/or interfaces for numerous reasons and would probaly sufer a considerable performance hit.
0
9703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10550
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10295
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10069
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7604
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6844
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3799
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.