# Good practices: power and log functions

This topic is 2948 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Its been suggested to me more than once to avoid the powf function because it is an expensive process. Does the same go for the log function? Why is it an expensive process for the processor?

Nikos

##### Share on other sites
The processor doesn't have native functions for pow and log so it ends up calculating it with some sort of Taylor approximation which is rather expensive.

##### Share on other sites

The processor doesn't have native functions for pow and log so it ends up calculating it with some sort of Taylor approximation which is rather expensive.

so is the log() to be avoided just as much as the pow()?

##### Share on other sites
Meh. Write what's clearest and most natural and only rewrite things if a profiler tells you something's a bottleneck.

##### Share on other sites
What SiCrane says. Also, the idea that powf is slow probably comes from times when compilers did something very naive with it. These days, a compiler will probably issue the same code for powf(x,2) as for x*x.

##### Share on other sites
Optimisation needs to include the optimisation of the programmer's time. If log or pow makes most sense to write, then write it until the profiler tells you not do.

##### Share on other sites
Basically what everyone else said. Don't try to second-guess the compiler; it will only end in tears.

##### Share on other sites
thanks for all the responses. I will approach it that way. Will make my life so much easier:)

##### Share on other sites

What SiCrane says. Also, the idea that powf is slow probably comes from times when compilers did something very naive with it. These days, a compiler will probably issue the same code for powf(x,2) as for x*x.

Actually they very probably don't. We went through this recently on a different forum.
To back that statement I just tried it in VS2008 Express with full optimisations on in release build and with code written such that it cannot be optimised out. There was a 14x speed difference between using powf(x, 2) and x*x.

That way of thinking works for a lot of things, but the math library routines are not one of them. Avoid using overly flexible functions when the simpler one will do. E.g. use sqrt(x) instead of pow(x, 0.5) because the former is specific and the later has to cater for the general case which means extra work.

The bottom line is, avoid log, pow, or other math library routines etc only where there is a clear, simple, and obvious alternative that doesn't require as much generality as those methods provide.

##### Share on other sites

[quote name='alvaro' timestamp='1319313688' post='4875421']
What SiCrane says. Also, the idea that powf is slow probably comes from times when compilers did something very naive with it. These days, a compiler will probably issue the same code for powf(x,2) as for x*x.

Actually they very probably don't. We went through this recently on a different forum.
To back that statement I just tried it in VS2008 Express with full optimisations on in release build and with code written such that it cannot be optimised out. There was a 14x speed difference between using powf(x, 2) and x*x.[/quote]

I checked shortly after I posted that comment, just to make sure that I am not spreading false information. gcc 4.6.1 seems to be smarter about this.

In C:
#include <math.h> float f(float x) { return powf(x, 2); } 
The assembly produced is:
 .file "kk.c" .text .p2align 4,,15 .globl f .type f, @function f: .LFB3: .cfi_startproc mulss %xmm0, %xmm0 // Hard to optimize beyond this ret .cfi_endproc .LFE3: .size f, .-f .ident "GCC: (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1" .section .note.GNU-stack,"",@progbits 

• 9
• 15
• 9
• 9
• 56