# logarithm and exponent computation performance

Using glibc on a x86 processor, which takes more CPU time? $a\ log\ b$ or $b^a$? For which values of $a$ is one faster than the other? Optional: Does the base used matter?

Because I know someone will mention it, I know that $a\ log\ b$ does not equal $b^a$.

• It will completely depend on what library is being used because it could be implemented differently in different libraries. Neither of those functions is a single CPU instruction on the typical RISC processors used today. I'm guessing you are thinking X86, PPC, ARM etc. – MPW Jan 29 '14 at 5:24
• The RTOS called VxWorks provides a method to time the execution of any routine with extreme accuracy by repeating the call a large number of times until the timing is certain within a very small margin of error. You may want to investigate such algorithms on various other operating systems that support multiple platforms... – MPW Jan 29 '14 at 5:28
• I updated the question to refer to glibc and x86. – stuckintheshuck Jan 29 '14 at 17:13

Look through the code used by the FreeBSD operating system:

It is claimed that these are rather high quality algorithms, better than cephes, and probably better than glibc.

http://lists.freebsd.org/pipermail/freebsd-numerics/

http://lists.freebsd.org/pipermail/freebsd-numerics/2012-September/000285.html

In one of these emails, someone describes an algorithm where they start with Taylor's series, and then run it through an optimization procedure to fine tune the coefficients. But there are so many emails that it would take a while to find where they describe it. These guys are really wanting to get every last digit accurate.

Update: I think the algorithm is called Remez algorithm. You can read about it on wikipedia.

I just ran a test in Python 2.7 (with optimization turned off) in Ubuntu 12 on a VM running on a 64 bit Xeon. It appears that for $|a|=\{0,1,2\}$ computing the exponent is slightly faster if not the same. For all other values of $a$, computing the log is faster. The value of b doesn't seem to matter.

This is only for the scenario I mentioned above. Feel free to run this same test on other platforms/architectures and post your results here.

import math
from datetime import datetime

a, b = 2, 33

then = datetime.now()
for _ in xrange(10000000):
y = a * math.log(b)
print datetime.now() - then

then = datetime.now()
for _ in xrange(10000000):
y = math.pow(b, a)
print datetime.now() - then