[Numpy-discussion] -ffast-math

Julian Taylor jtaylor.debian at googlemail.com
Sun Dec 1 17:16:40 EST 2013

On 01.12.2013 22:59, Dan Goodman wrote:
> Julian Taylor <jtaylor.debian <at> googlemail.com> writes:
>> your sin and exp calls are loop invariants, they do not depend on the
>> loop iterable.
>> This allows to move the expensive functions out of the loop and only
>> leave some simple arithmetic in the body.
> Ahhhh! I feel extremely stupid for not realising this! Thanks Julian.
> Any thoughts on why using -ffast-math it actually goes slower for just doing
> sin(x)?

no on my linux machine ffast-math is a little faster:
numpy: 311 ms
weave_slow: 291 ms
weave_fast: 262 ms

here is a pure numpy version of your calculation which only performs 3
times worse than weave:

def timefunc_numpy2(a, v):
    ext = exp(-dt/tau)
    sit = sin(2.0*freq*pi*t)
    bs = 20000
    for i in range(0, N, bs):
        ab = a[i:i+bs]
        vb = v[i:i+bs]
        absit = ab*sit + b
        vb *= ext
        vb += absit
        vb -= absit*ext

it works by replacing temporaries with inplace operations and blocks the
operations to be more memory cache friendlier.
using numexpr should give you similar results.

More information about the NumPy-Discussion mailing list