| Jackson Marie (450) | |||
|
Hello everybody! Today...I have a question. I have no idea about the <math> function fabs(). Because, an example may prove :
From the example above, it seems the fabsf() function is redundant because it performs very slow and without it the basic C code still works very well (lighting speed IMO). Do you know why? | |||
|
Last edited on
|
|||
| Peter87 (3672) | |
|
fabsf is not standard as far as I know and I think it only works on float. You use double so it will have to convert between double and float a lot if you use it in your program. fabs is not the same as negation so it's not strange that fabs is slower. | |
|
|
|
| Disch (8337) | ||||
This. Your test is unfair. fabs has to actually check to see if the number is negative or not. Conditionals are significantly slower. Try this instead:
I'm sure that will be much more comparable. | ||||
|
|
||||
| Cubbi (1568) | ||
|
Check your compiled code. It's possible that the compiler removed negation completely and you've only measured the time it takes to perform 100000000 reads and writes to i in the first case, but didn't know how to remove the call to fabs(). When I make both fVal and fResult volatile doubles, my results are exactly identical across three compilers and two platforms.compiled code (Linux/clang++)
As you can see, the difference is between an AND and a XOR, which are unlikely to differ in execution time, the rest of the code is identical. | ||
|
Last edited on
|
||