Python speed vs csharp

Bengt Richter bokr at oz.net
Fri Aug 1 02:08:46 CEST 2003


On Wed, 30 Jul 2003 23:09:22 -0700, Mike <mike at nospam.com> wrote:

>Bear with me: this post is moderately long, but I hope it is relatively
>succinct. 
>
>I've been using Python for several years as a behavioral modeling tool for
>the circuits I design. So far, it's been a good trade-off: compiled C++
>would run faster, but the development time of Python is so much faster, and
>the resulting code is so much more reliable after the first pass, that I've
>never been tempted to return to C++. Every time I think stupid thoughts
>like, "I'll bet I could do this in C++," I get out my copy of Scott Meyers'
>"Effecive C++," and I'm quickly reminded why it's better to stick with
>Python (Meyers is a very good author, but points out lots of quirks and
>pitfalls with C++ that I keep thinking that I shouldn't have to worry
>about, much less try to remember). Even though Python is wonderful in that
>regard, there are problems.
>
>Here's the chunk of code that I'm spending most of my time executing:
>
<snip>

Some thoughts...

Is there any order to the values of x you call with? Or are they totally
unrelated? (I.e., can the last value give you a good approximation for the
next, knowing the step in the argument?  What are the limits to possible x values?
Have you tested how many terms in the approximation you really need to get usable results
(vs final tests)? |error| <= 1.5e-7 for all x sounds unnecessarily tight
for many engineering problems. What are the statistics of the errors on the
x values you are passing? Could you conceivably pre-compute a lookup table to
use instead of a function? E.g., this can be useful in turning a uniform distribution
into something else. Sometimes you can get away with a surprisingly small table for that.
(Plus you get to pre-eliminate weird outliers ;-)

Do you have many places in your code calling this function? 1.5 billion sounds
like mostly in a few places in some loops. If so, have you tried just putting
the approximation code in-line without a function call? That should save a
fair amount of time. Function calls are pretty expensive time-wise in python.
You can verify the relative cost by just returning a constant immediately from
the function.

Regards,
Bengt Richter




More information about the Python-list mailing list