Skip to content

Instantly share code, notes, and snippets.

@grahamwren
Last active February 12, 2020 22:55
Show Gist options
  • Save grahamwren/2b37cb47b9d784727619bc52e91f0812 to your computer and use it in GitHub Desktop.
Save grahamwren/2b37cb47b9d784727619bc52e91f0812 to your computer and use it in GitHub Desktop.
LENGTH=100000000
bench: test_boxing.exe
echo "System: $$(uname -a)" > results.log
for i in {0..10}; do (echo "run $$i"; ./test_boxing.exe) >> results.log; done
test_boxing.exe: test_boxing.cpp
g++ -O0 --std=c++11 -DLENGTH=$(LENGTH) test_boxing.cpp -o test_boxing.exe
System: Darwin Grahams-MacBook-Pro-2.local 19.2.0 Darwin Kernel Version 19.2.0: Sat Nov 9 03:47:04 PST 2019; root:xnu-6153.61.1~20/RELEASE_X86_64 x86_64
run 0
test_1: primitive and alloc runtime: 357.496 ms
test_2: boxed and alloc runtime: 1010.83 ms
test_3: primitive and no-alloc runtime: 342.527 ms
test_4: boxed and no-alloc runtime: 209.27 ms
run 1
test_1: primitive and alloc runtime: 342.769 ms
test_2: boxed and alloc runtime: 993.804 ms
test_3: primitive and no-alloc runtime: 336.365 ms
test_4: boxed and no-alloc runtime: 213.019 ms
run 2
test_1: primitive and alloc runtime: 332.564 ms
test_2: boxed and alloc runtime: 957.733 ms
test_3: primitive and no-alloc runtime: 337.576 ms
test_4: boxed and no-alloc runtime: 203.382 ms
run 3
test_1: primitive and alloc runtime: 338.737 ms
test_2: boxed and alloc runtime: 972.019 ms
test_3: primitive and no-alloc runtime: 345.216 ms
test_4: boxed and no-alloc runtime: 216.245 ms
run 4
test_1: primitive and alloc runtime: 334.753 ms
test_2: boxed and alloc runtime: 967.459 ms
test_3: primitive and no-alloc runtime: 379.589 ms
test_4: boxed and no-alloc runtime: 217.279 ms
run 5
test_1: primitive and alloc runtime: 347.914 ms
test_2: boxed and alloc runtime: 1179.76 ms
test_3: primitive and no-alloc runtime: 359.037 ms
test_4: boxed and no-alloc runtime: 204.748 ms
run 6
test_1: primitive and alloc runtime: 326.357 ms
test_2: boxed and alloc runtime: 1034.79 ms
test_3: primitive and no-alloc runtime: 394.06 ms
test_4: boxed and no-alloc runtime: 230.615 ms
run 7
test_1: primitive and alloc runtime: 374.186 ms
test_2: boxed and alloc runtime: 1033.29 ms
test_3: primitive and no-alloc runtime: 355.135 ms
test_4: boxed and no-alloc runtime: 206.27 ms
run 8
test_1: primitive and alloc runtime: 382.636 ms
test_2: boxed and alloc runtime: 1076.76 ms
test_3: primitive and no-alloc runtime: 408.046 ms
test_4: boxed and no-alloc runtime: 226.272 ms
run 9
test_1: primitive and alloc runtime: 392.07 ms
test_2: boxed and alloc runtime: 1187.21 ms
test_3: primitive and no-alloc runtime: 376.981 ms
test_4: boxed and no-alloc runtime: 228.64 ms
run 10
test_1: primitive and alloc runtime: 340.865 ms
test_2: boxed and alloc runtime: 1024.61 ms
test_3: primitive and no-alloc runtime: 349.574 ms
test_4: boxed and no-alloc runtime: 224.538 ms
#include <chrono>
#include <iostream>
using namespace std;
#ifndef LENGTH
#define LENGTH 100 * 1000 * 1000
#endif
class Box {
public:
bool missing = false;
};
class Int : public Box {
public:
int val;
};
int main() {
// TEST 1: Primitive
auto t1 = chrono::high_resolution_clock::now();
int *a = new int[LENGTH];
for (int i = 0; i < LENGTH; i++) {
a[i] = LENGTH - i;
}
auto t2 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> diff = t2 - t1;
cout << " test_1: primitive and alloc runtime: " << diff.count() << " ms"
<< endl;
delete[] a;
// TEST 2: Boxed
auto t3 = chrono::high_resolution_clock::now();
Int *a2 = new Int[LENGTH];
for (int i = 0; i < LENGTH; i++) {
a2[i].val = LENGTH - i;
}
auto t4 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> diff2 = t4 - t3;
cout << " test_2: boxed and alloc runtime: " << diff2.count() << " ms"
<< endl;
delete[] a2;
// TEST 3: Primitive
int *a3 = new int[LENGTH];
auto t5 = chrono::high_resolution_clock::now();
for (int i = 0; i < LENGTH; i++) {
a3[i] = LENGTH - i;
}
auto t6 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> diff3 = t6 - t5;
cout << " test_3: primitive and no-alloc runtime: " << diff3.count() << " ms"
<< endl;
delete[] a3;
// TEST 4: Boxed
Int *a4 = new Int[LENGTH];
auto t7 = chrono::high_resolution_clock::now();
for (int i = 0; i < LENGTH; i++) {
a4[i].val = LENGTH - i;
}
auto t8 = chrono::high_resolution_clock::now();
chrono::duration<double, milli> diff4 = t8 - t7;
cout << " test_4: boxed and no-alloc runtime: " << diff4.count() << " ms"
<< endl;
delete[] a4;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment