Ask Your Question
0

speed and order of operations with CDF

asked 2012-11-18 19:37:08 +0100

marco gravatar image

updated 2012-11-18 20:50:50 +0100

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 flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2012-11-20 00:29:53 +0100

Jason Grout gravatar image

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()
[('load_arg', 0), 'exp', 'abs', 'return']
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
edit flag offensive delete link more

Comments

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

marco gravatar imagemarco ( 2012-11-20 23:18:25 +0100 )edit
1

answered 2012-11-18 19:58:13 +0100

benjaminfjones gravatar image

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
edit flag offensive delete link more

Comments

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.

marco gravatar imagemarco ( 2012-11-18 20:34:16 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2012-11-18 19:37:08 +0100

Seen: 360 times

Last updated: Nov 20 '12