Created
February 14, 2012 16:51
-
-
Save sonney2k/1828072 to your computer and use it in GitHub Desktop.
no difference
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*g++ -O9 -mfpmath=sse -march=native -mtune=native nodifference.cpp */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <iostream> | |
#include <vector> | |
#include <cmath> | |
#include <sys/time.h> | |
using namespace std; | |
static double time() { | |
timeval tv; | |
if (gettimeofday(&tv, NULL)==0) | |
return tv.tv_sec+(tv.tv_usec*1e-6); | |
return 0; | |
} | |
static double | |
JS1 (const vector<double>& a, const vector<double>& b) { | |
double result = 0; | |
for (size_t i = 0; i < a.size (); ++i) { | |
double a_i = 0, b_i = 0; | |
double ab = a[i]+b[i]; | |
if (a[i] != 0) | |
a_i = a[i]/2 * log2(ab/a[i]); | |
if (b[i] != 0) | |
b_i = b[i]/2 * log2(ab/b[i]); | |
result += a_i + b_i; | |
} | |
return result; | |
} | |
static double | |
JS2 (const vector<double>& a, const vector<double>& b) { | |
double result = 0; | |
for (size_t i = 0; i < a.size (); ++i) { | |
double a_i = 0, b_i = 0; | |
double ab = a[i]+b[i]; | |
if (a[i] != 0) | |
a_i = a[i] * log2(ab/a[i]); | |
if (b[i] != 0) | |
b_i = b[i] * log2(ab/b[i]); | |
result += 0.5*(a_i + b_i); | |
} | |
return result; | |
} | |
static double | |
diffTime (const double start, const double end) { | |
return end-start; | |
} | |
static void | |
random_init (size_t size, vector<double>& a, vector<double>& b) { | |
a.reserve (size); | |
b.reserve (size); | |
for (int i = 0; i < size; ++i) { | |
a.push_back (rand()%100); | |
b.push_back (rand()%100); | |
} | |
} | |
int | |
main (int argc, char** argv) { | |
double t1, t2; | |
size_t size = 1e7; | |
size_t runTimes = 1e3; | |
vector<double> a; | |
vector<double> b; | |
a.clear (), b.clear (); | |
random_init (size, a, b); | |
a[0]=1; | |
b[0]=1; | |
double diff1 = 0.0; | |
for (int i = 0; i < runTimes; ++i) { | |
t1 = time (); | |
JS1 (a, b); | |
t2 = time (); | |
diff1 += diffTime (t1, t2); | |
a[0]+=1; | |
b[0]+=1; | |
} | |
printf("JS1: %10.10g\n", diff1); | |
double diff2 = 0.0; | |
a[0]=1; | |
b[0]=1; | |
for (int i = 0; i < runTimes; ++i) { | |
t1 = time (); | |
JS2 (a, b); | |
t2 = time (); | |
diff2 += diffTime (t1, t2); | |
a[0]+=1; | |
b[0]+=1; | |
} | |
printf("JS2: %10.10g\n", diff2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment