views:

177

answers:

2

I calculated time elapsed in my function in VS and g++. I see a huge performance difference. I use unix time command to calculate time elapsed from my g++ compiled executable. I use clock function in main() to calculate time elapsed in VS. g++ is giving ~2 seconds whereas VS is giving 19 seconds which is a huge difference. Can this be expected? Can you see something that could be cause of problem here?

Both run on the same computer and with same flags (-O2).

+1  A: 

Can this be expected?

No, this is not expected and is likely caused by a configuration issue (such as building a debug configuration opposed to a release configuration).

Can you see something that could be cause of problem here?

Where is 'here'? you didn't post anything for us to look for problems in. You could post the command lines for both to start.

BioBuckyBall
A: 

If I'm understanding you right, you are saying that in g++ you are using the time() function.

This gives the number of seconds since 1970 e.g. time_t t = time(NULL);

Under Visual Studio C++ you say you are using the clock function correct? Well clock() returns the number of clock ticks elapsed since the program was launched.

To get an equivalent you need to utilize the CLOCKS_PER_SEC macro to get it to relate to seconds. Are you doing that? This could entirely explain the different result.

If this is not the case then perhaps you better post some code to show us what you're doing.

In any case, why are you using different timing functions for each compiler? both functions are available in both compilers. Also, time doesn't give you sub-second accuracy, clock does but also is not very accurate. In fact, neither are very accurate. Have you considered using the high resolution timer functions available in the windows API?

Matt H
CLOCKS_PER_SEC is usually defined as 1000000 and does not depend on CPU speed, so it is not a very good idea to use it. It is better to use gettimeofday() to calculate performance.
Vlad Lazarenko
@vlad, totally agree.
Matt H