Skip to content

Commit

Permalink
add sequence benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
gombiuda committed Feb 7, 2014
1 parent baf6787 commit 1694b87
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ test_server: $(OBJS) test_server.o
benchmark: $(OBJS) benchmark.o
g++ -pthread -o benchmark benchmark.o $(OBJS)

benchmark_sequence: $(OBJS) benchmark_sequence.o
g++ -pthread -o benchmark_sequence benchmark_sequence.o $(OBJS)

benchmark_train: $(OBJS) benchmark_train.o
g++ -o benchmark_train benchmark_train.o $(OBJS)

Expand Down
36 changes: 36 additions & 0 deletions benchmark_sequence.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <iostream>
#include <thread>
#include <sys/time.h>
#include "sequence.h"

using namespace std;

#define C 2
#define N 65536
#define MILLION 1000000

void loop(Sequence *sequence, int n) {
for (int i = 0; i < n; i++) {
sequence->set(sequence->get());
}
}

int main() {
cout << "size: " << sizeof(Sequence) << endl;
Sequence *sequences = new Sequence[C]();
thread threads[C];
struct timeval start, end;
gettimeofday(&start, NULL);
for (int i = 0; i < C; i++) {
threads[i] = thread(loop, sequences + i, N);
}
for (int i = 0; i < C; i++) {
threads[i].join();
}
gettimeofday(&end, NULL);
double use_time = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / (double)MILLION;
cout << "Process " << C * N << " operations" << endl;
cout << "Use time: " << use_time << " s" << endl;
cout << "ops: " << dec << (long)(C * N / use_time) << endl;
return 0;
}
1 change: 1 addition & 0 deletions sequence.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ class Sequence {
Sequence(long value);
static int INITIAL_VALUE;
long value;
long padding[7]; // avoid false sharing
};

0 comments on commit 1694b87

Please sign in to comment.