Good practices: power and log functions

Started by
14 comments, last by rip-off 12 years, 5 months ago
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
Advertisement
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.

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()?
Meh. Write what's clearest and most natural and only rewrite things if a profiler tells you something's a bottleneck.
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.
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.
Basically what everyone else said. Don't try to second-guess the compiler; it will only end in tears.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

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

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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms

[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

This topic is closed to new replies.

Advertisement