Python speed vs csharp
David M. Cooke
cookedm+news at physics.mcmaster.ca
Thu Jul 31 21:50:18 EDT 2003
At some point, "Terry Reedy" <tjreedy at udel.edu> wrote:
> "David M. Cooke" <cookedm+news at physics.mcmaster.ca> wrote in message
> news:qnkn0eutndk.fsf at arbutus.physics.mcmaster.ca...
>> At some point, Mike <mike at nospam.com> wrote:
>> > Here's the chunk of code that I'm spending most of my time
> executing:
>> >
>> > # Rational approximation for erfc(x) (Abramowitz & Stegun, Sec.
> 7.1.26)
>> > # Fifth order approximation. |error| <= 1.5e-7 for all x
>> > #
>> > def erfc( x ):
>> > p = 0.3275911
>> > a1 = 0.254829592
>> > a2 = -0.284496736
>> > a3 = 1.421413741
>> > a4 = -1.453152027
>> > a5 = 1.061405429
>> >
>> > t = 1.0 / (1.0 + p*float(x))
>> > erfcx = ( (a1 + (a2 + (a3 +
>> > (a4 + a5*t)*t)*t)*t)*t ) * math.exp(-(x**2))
>> > return erfcx
>
> Does inlining the constants give any speedup? or is local lookup so
> fast that it does not matter?
Should've thought of that too :-). It runs in 6.07 s now (my previous
version was 6.45 s, and the unmodified version above was 8.23 s).
Here's my best version:
exp = math.exp
def erfc( x ):
t = 1.0 + 0.3275911*x
return ( (0.254829592 + (
(1.421413741 + (-1.453152027 + 1.061405429/t)/t)/t - 0.284496736)
/t)/t ) * exp(-(x**2))
The reversal of the order of the a2 evaluation is because looking at
the bytecodes (using the dis module) it turns out that -0.284 was
being stored as 0.284, then negated. However, -1.45 is stored as that.
This runs in 5.77 s. You could speed that up a little if you're
willing to manipulate the bytecode directly :-)
--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
More information about the Python-list
mailing list