Skip to content

Instantly share code, notes, and snippets.

@sonney2k
Created February 14, 2012 16:48
Show Gist options
  • Save sonney2k/1828051 to your computer and use it in GitHub Desktop.
Save sonney2k/1828051 to your computer and use it in GitHub Desktop.
test running time of 2 diff JS kernel counting
#include <iostream>
#include <vector>
#include <cmath>
#include <time.h>
using namespace std;
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 clock_t start, const clock_t end) {
double diffTicks = end-start;
double inNS = (diffTicks*1e6)/CLOCKS_PER_SEC;
return inNS;
}
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) {
clock_t t1, t2;
size_t size = 1e6;
size_t runTimes = 1e3;
vector<double> a;
vector<double> b;
double diff = 0.0;
for (int i = 0; i < runTimes; ++i) {
a.clear (), b.clear ();
random_init (size, a, b);
t1 = clock ();
JS1 (a, b);
t2 = clock ();
diff += diffTime (t1, t2);
}
cout << "JS1: " << diff << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment