Skip to content

Commit

Permalink
Prep new post
Browse files Browse the repository at this point in the history
  • Loading branch information
lemire committed Jan 31, 2013
1 parent c914b79 commit 2debf68
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
86 changes: 86 additions & 0 deletions 2013/01/31/dynarray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@


#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <iostream>
#include <limits>
#include <vector>

using namespace std;

class WallClockTimer {
public:
struct timeval t1, t2;
WallClockTimer() :
t1(), t2() {
gettimeofday(&t1, 0);
t2 = t1;
}
void reset() {
gettimeofday(&t1, 0);
t2 = t1;
}
int elapsed() {
return ((t2.tv_sec - t1.tv_sec) * 1000) + ((t2.tv_usec - t1. tv_usec)
/ 1000);
}
int split() {
gettimeofday(&t2, 0);
return elapsed();
}
};


int test(size_t N) {
vector<int> x;
for(size_t i = 0 ; i < N ; ++i ) {
x.push_back(i);
}
return x.back();
}


int testManual(size_t N, const int multiplier, const int div = 1, const int constant = 0) {
int * x = new int[1];
size_t s = 1;
for(size_t i = 0 ; i < N ; ++i ) {
if(i == s) {
int * nx = new int[ multiplier * s / div + constant ];
memcpy(nx,x,s*sizeof(int));
delete[] x;
x = nx;
s = multiplier * s / div + constant;
}
x[i] = i;
}
return x[N-1];
}



int overall(size_t N) {
int bogus = 0;
WallClockTimer t;
t.reset();
bogus += test(N);
int delay = t.split();
cout << "STL vector " << N /(delay * 1000.0) << endl;

for(size_t factor = 1; factor <= 6; ++ factor) {
t.reset();
bogus += testManual(N,2+factor,2,factor&1==0 ? 0 : 1);
delay = t.split();
cout << "pointer-based "<< (factor +2)/2.0<< " : " << N /(delay * 1000.0) << endl;
}
return bogus;
}

int main() {
int bogus = overall(1024*1024);
bogus += overall(1024*1024);
bogus += overall(1024*1024*64);
bogus += overall(1024*1024*64);

return bogus;
}
3 changes: 2 additions & 1 deletion extra/integerlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ using namespace std;
class WallClockTimer {
public:
struct timeval t1, t2;
WallClockTimer() :

() :
t1(), t2() {
gettimeofday(&t1, 0);
t2 = t1;
Expand Down

0 comments on commit 2debf68

Please sign in to comment.