EDIT: considering the first answer I removed the "myexp()" function as with bug and not the main point of the discussion
I have one simple piece of code and compiled for different platform and get different performance results (execution time):
Java 8 / Linux: 3.5 seconds
Execution Command:
java -server Test
C++ / gcc 4.8.3: 6.22 seconds
Compilation options:
O3
C++ / Visual Studio 2015: 1.7 seconds
Compiler Options:
/Og /Ob2 /Oi
It seems that VS has these additional options not available for g++ compiler.
My question is: why is Visual Studio (with those compiler options) so faster with respect to both Java and C++ (with O3 optimization, which I believe is the most advanced)?
Below you can find both Java and C++ code.
C++ Code:
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cmath>
static unsigned int g_seed;
//Used to seed the generator.
inline void fast_srand( int seed )
{
g_seed = seed;
}
//fastrand routine returns one integer, similar output value range as C lib.
inline int fastrand()
{
g_seed = ( 214013 * g_seed + 2531011 );
return ( g_seed >> 16 ) & 0x7FFF;
}
int main()
{
static const int NUM_RESULTS = 10000;
static const int NUM_INPUTS = 10000;
double dInput[NUM_INPUTS];
double dRes[NUM_RESULTS];
fast_srand(10);
clock_t begin = clock();
for ( int i = 0; i < NUM_RESULTS; i++ )
{
dRes[i] = 0;
for ( int j = 0; j < NUM_INPUTS; j++ )
{
dInput[j] = fastrand() * 1000;
dInput[j] = log10( dInput[j] );
dRes[i] += dInput[j];
}
}
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
printf( "Total execution time: %f sec - %f\n", elapsed_secs, dRes[0]);
return 0;
}
Java Code:
import java.util.concurrent.TimeUnit;
public class Test
{
static int g_seed;
static void fast_srand( int seed )
{
g_seed = seed;
}
//fastrand routine returns one integer, similar output value range as C lib.
static int fastrand()
{
g_seed = ( 214013 * g_seed + 2531011 );
return ( g_seed >> 16 ) & 0x7FFF;
}
public static void main(String[] args)
{
final int NUM_RESULTS = 10000;
final int NUM_INPUTS = 10000;
double[] dRes = new double[NUM_RESULTS];
double[] dInput = new double[NUM_INPUTS];
fast_srand(10);
long nStartTime = System.nanoTime();
for ( int i = 0; i < NUM_RESULTS; i++ )
{
dRes[i] = 0;
for ( int j = 0; j < NUM_INPUTS; j++ )
{
dInput[j] = fastrand() * 1000;
dInput[j] = Math.log( dInput[j] );
dRes[i] += dInput[j];
}
}
long nDifference = System.nanoTime() - nStartTime;
System.out.printf( "Total execution time: %f sec - %f\n", TimeUnit.NANOSECONDS.toMillis(nDifference) / 1000.0, dRes[0]);
}
}