Skip to content

Some of the changes made to the code to make it more clean #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
139 changes: 74 additions & 65 deletions cct.cc
Original file line number Diff line number Diff line change
@@ -1,88 +1,97 @@
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <cstdlib>
#include <sys/time.h>

using namespace std;

long ms() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec % 86400) * 1000 + (tv.tv_usec/1000);
long getCurrentTimeInMilliseconds() {
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec % 86400) * 1000 + (tv.tv_usec / 1000);
}

static int vs[1000];

static long zero = 0;
static long one = 0;

void do_s(int x) {
switch(x) {
case 0: zero++; break;
case 1: one++; break;
}
const int ARRAY_SIZE = 1000;
int switchValues[ARRAY_SIZE];
long zeroCount = 0;
long oneCount = 0;

void incrementSwitchValue(int value) {
switch (value) {
case 0:
zeroCount++;
break;
case 1:
oneCount++;
break;
}
}


void s_lup(int n) {
for (int i=0; i<n; i++) {
do_s(vs[i%1000]);
}
void performSwitchIncrements(long numberOfIncrements) {
for (int i = 0; i < numberOfIncrements; i++) {
incrementSwitchValue(switchValues[i % ARRAY_SIZE]);
}
}

class B {
public:
virtual void inc() = 0;
class BaseClass {
public:
virtual void increment() = 0;
};

static B* bs[1000];
BaseClass* classValues[ARRAY_SIZE];

class D : public B {
public:
virtual void inc() {zero++;}
class DerivedClassZero : public BaseClass {
public:
virtual void increment() { zeroCount++; }
};

class E : public B {
public:
virtual void inc() {one++;}
class DerivedClassOne : public BaseClass {
public:
virtual void increment() { oneCount++; }
};

void do_c(B* b) {
b->inc();
void incrementClassValue(BaseClass* value) {
value->increment();
}

void c_lup(int n) {
for (int i=0; i<n; i++) {
do_c(bs[i%1000]);
}
void performClassIncrements(long numberOfIncrements) {
for (int i = 0; i < numberOfIncrements; i++) {
incrementClassValue(classValues[i % ARRAY_SIZE]);
}
}

int main(int ac, char** av)
{
srand(time(NULL));

long n = atol(av[1]);

for (int i=0; i<1000; i++) {
vs[i] = rand()%2;
bs[i] = rand()%2 ? (B*)new E() : (B*)new D();
}
zero=one=0;
long start = ms();
s_lup(n);
long end_s = ms();
if (one != zero) cout<< "s-lup mismatch: " << zero << " " << one << endl;
zero=one=0;
c_lup(n);
if (one != zero) cout<< "c-lup mismatch: " << zero << " " << one << endl;
long end_c = ms();
int s_time = (end_s - start);
int c_time = (end_c - end_s);
int diff = (c_time - s_time);
double s_ns = (s_time * 1000000.0) / n;
double c_ns = (c_time * 1000000.0) / n;
double diff_ns = (diff * 1000000.0) / n;
printf("switch: %d, class: %d, diff: %d;\n",s_time, c_time, diff);
printf("switch-ns: %g, class-ns: %g, diff-ns: %g\n", s_ns, c_ns, diff_ns);
int main(int argc, char** argv) {
srand(time(NULL));

long numberOfIncrements = atol(argv[1]);

for (int i = 0; i < ARRAY_SIZE; i++) {
switchValues[i] = rand() % 2;
classValues[i] = rand() % 2 ? (BaseClass*) new DerivedClassOne() : (BaseClass*) new DerivedClassZero();
}

zeroCount = oneCount = 0;
long startTime = getCurrentTimeInMilliseconds();
performSwitchIncrements(numberOfIncrements);
long endTimeSwitch = getCurrentTimeInMilliseconds();

if (oneCount != zeroCount)
cout << "Switch mismatch: " << zeroCount << " " << oneCount << endl;

zeroCount = oneCount = 0;
performClassIncrements(numberOfIncrements);

if (oneCount != zeroCount)
cout << "Class mismatch: " << zeroCount << " " << oneCount << endl;

long endTimeClass = getCurrentTimeInMilliseconds();

int switchTime = (endTimeSwitch - startTime);
int classTime = (endTimeClass - endTimeSwitch);

printf("Switch: %d, Class: %d\n", switchTime, classTime);

double switchTimePerIncrement = (switchTime * 1000000.0) / numberOfIncrements;
double classTimePerIncrement = (classTime * 1000000.0) / numberOfIncrements;

printf("Switch time per increment: %g ns, Class time per increment: %g ns\n", switchTimePerIncrement, classTimePerIncrement);
}