Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
#define ARRAY_SIZE 500000
#define ACCESS_AMOUNT 5000000
#ifdef packitbaby
#pragma pack(push, 1)
#endif
struct value {
char type;
float v;
};
#ifdef packitbaby
#pragma pack(pop)
#endif
struct dmlist {
size_t num;
value * values;
};
dmlist *listoflists[ARRAY_SIZE];
long counter = 0;
int main () {
cout << "Hello world" << endl;
srand ( time(NULL) ); //initialize the random seed
cout << "value size: " << sizeof(value) << endl;
cout << "dmlist size: " << sizeof(dmlist) << endl;
#ifdef packitbaby
cout << "Using packed format" << endl;
#endif
system( "PAUSE" );
cout << "Generating array" << endl;
clock_t t = clock();
for (size_t i = 0; i < ARRAY_SIZE; i++) {
dmlist * L = new dmlist;
L->num = rand() % 500;
L->values = new value[L->num];
for (size_t o = 0; o < L->num; o++) {
value entry = L->values[o];
entry.type = (char)(rand() % 255);
entry.v = (float)rand() * (float)rand() / (float)RAND_MAX;
L->values[o] = entry;
}
listoflists[i] = L;
}
t = clock() - t;
cout << "It took " << ((float)t)/CLOCKS_PER_SEC << " seconds to generate " << ARRAY_SIZE << " random lists" << endl;
system( "PAUSE" );
cout << "Accessing array" << endl;
t = clock();
for (size_t i = 0; i < ACCESS_AMOUNT; i++) {
dmlist * L = listoflists[rand() % ARRAY_SIZE];
for (size_t o = 0; o < L->num; o++) {
value entry = L->values[rand() % L->num];
if (((int)entry.type) % 2)
counter++;
if (((int)entry.v) % 2)
counter++;
}
}
t = clock() - t;
cout << "It took " << ((float)t)/CLOCKS_PER_SEC << " seconds to access " << ACCESS_AMOUNT << " random lists" << endl;
cout << "Counter:" << counter << endl;
system( "PAUSE" );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.