# Including cmath makes exp much slower

## Recommended Posts

LonelyStar    192
Hi, I have a strange problem: I have a program using exp a lot. If I do: #include <math.h> and use exp, the runtime of the program is 1 second. If I do: #include <cmath> and use std::exp, the runtime of the program is 4 seconds. If I do: #include <cmath> #include <math.h> and use exp, the runtime of the program is 4 seconds. The problem is, that I use libraries, which include cmath (even in another source file) and thereby my program gets slower. I tried to make a simple test case, but the problem did not appear there. So I do not know which part of my pretty large program triggers this. Anyone ideas and/or experience with this? Thanks! nathan

##### Share on other sites
jjd    2140
Are you seeing this in debug builds? Trying using release if you are.

##### Share on other sites
LonelyStar    192
I just reconfirmed. I see this, compiling with gcc 4.4.2 (x86_64) using -O2 -NDEBUG

##### Share on other sites
alvaro    21266
Can you please post a complete sample program that shows the problem? I can probably figure out what's going on if I can play with it a bit.

EDIT: Nevermind. I missed the part where you said you didn't manage to reproduce the problem in a small program. My best guess is that you are converting between floats and doubles, or something like that. Perhaps try to keep the same exact types in the sample program.

##### Share on other sites
LonelyStar    192
Hi,

What A nice idea, this seems to be it. Here is a test program:
    1 #include <math.h>    2 #include <cmath> //Insert or remove this line to see the difference    3     4 using namespace std;    5     6 int main()    7 {       8     float index=0.0;    9     for(int i=0;i<1E8;++i)   10     {      11         index=exp(-index);   12     }   13     return 0;   14 }

I probably can fix this, by removing all "using namespace std" (working on it ...).

But why can std::exp not handle floats proabably?

Thanks!
Nathan

##### Share on other sites
cache_hit    614
Not sure how gcc implements <cmath>, but on Visual Studio, it's defined like this:

namespace std
{
using ::exp;
}

So I'm not really sure why you'd see a difference if the only thing you were doing differently was using std::exp vs ::exp, since they are actually the same function.

Maybe the inclusion of cmath is bringing in a bunch of other header files which have static variables that require static initialization that takes some time. 3 seconds of static initialization time still seems like a lot though

##### Share on other sites
visitor    643
Can't see any performance difference.

However the <math.h> version only works with doubles. The float version is only overloaded in <cmath>. What if you tried with doubles?

##### Share on other sites
cache_hit    614
Ahh I guess that's the difference. Visual Studio MSVCRT has overloaded versions for float, double, and long double in math.h.

##### Share on other sites
LonelyStar    192
Quote:
 Original post by visitorCan't see any performance difference.However the version only works with doubles. The float version is only overloaded in . What if you tried with doubles?

If I do it with double, the difference vanishes.