# speed and order of operations with CDF

Why is the absolute value of the exponential of z:

f = fast_callable(exp(z).abs(),domain=CDF,vars='z')


about twice as fast as the exponential of the real part of z:

g = fast_callable(exp(z.real()), domain=CDF, vars='z')


Should I ignore this kind of thing in sage, or is there a good reason in this particular case?

Data:

z = var('z')
f = fast_callable(exp(z).abs(),domain=CDF,vars='z')
g = fast_callable(exp(z.real()), domain=CDF, vars='z')
timeit('f(4+2*I)')


625 loops, best of 3: 2.94 µs per loop

timeit('g(4+2*I)')


625 loops, best of 3: 5.87 µs per loop

Non-fast_callable times, in case you are interested:

z = var('z')
fs(z) = exp(z).abs()
gs(z) = exp(z.real())
timeit('fs(4+2*I)')


625 loops, best of 3: 1.02 ms per loop

timeit('gs(4+2*I)')


625 loops, best of 3: 988 µs per loop

edit retag close merge delete

Sort by » oldest newest most voted

Notice that g is making a python function call, while f is accomplishing its task without any python function calls:

sage: z = var('z')
sage: f = fast_callable(exp(z).abs(),domain=CDF,vars='z')
sage: g = fast_callable(exp(z.real()), domain=CDF, vars='z')
sage: f.python_calls()
[]
sage: g.python_calls()
[real_part]
sage: f.op_list()
sage: g.op_list()
[('load_arg', 0), ('py_call', real_part, 1), 'exp', 'return']


However, I get timings that are pretty equivalent:

sage: a=4+2*I
sage: timeit('f(a)')
625 loops, best of 3: 385 µs per loop
sage: timeit('g(a)')
625 loops, best of 3: 386 µs per loop

more

very interesting, I didn't know about python_calls and op_list!

Can you be more explicit with the code you are timing? What calculation involving f and g takes 1 second w/o fast_callable? I get similar timing for both functions at $z = 4 + 2I$.

sage: z = var('z')
sage: f = fast_callable(exp(z).abs(),domain=CDF,vars='z')
sage: g = fast_callable(exp(z.real()), domain=CDF, vars='z')
sage: f(4+2*I)
54.5981500331
sage: g(4+2*I)
54.5981500331
sage: timeit('f(4+2*I)')
625 loops, best of 3: 592 µs per loop
sage: timeit('g(4+2*I)')
625 loops, best of 3: 595 µs per loop

more

I put the data up now, I made an error about the non-fast_callable times, but the fast_callable times are as shown above.