Skip to content

Instantly share code, notes, and snippets.

@karimnaaji
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karimnaaji/851511e0fdb40619d47b to your computer and use it in GitHub Desktop.
Save karimnaaji/851511e0fdb40619d47b to your computer and use it in GitHub Desktop.
auto pointers bench
#include <iostream>
#include <ctime>
#include <climits>
#include <memory>
int main() {
struct big_struct {
long long array[10];
};
clock_t b0 = clock();
for (long i = 0; i < 300; ++i) {
big_struct* a = new big_struct();
delete a;
}
std::cout << "raw alloc: " << (float(clock() - b0) / CLOCKS_PER_SEC) * 1000 << "ms" << std::endl;
clock_t b1 = clock();
for (long i = 0; i < 300; ++i) {
std::shared_ptr<big_struct> a(new big_struct());
}
std::cout << "auto alloc: " << (float(clock() - b1) / CLOCKS_PER_SEC) * 1000 << "ms" << std::endl;
std::cout << sizeof(long long long) << std::endl;
return 0;
}
#include <memory>
#include <iostream>
struct A {
long long a;
};
using namespace std;
#define N 1000
int main() {
clock_t t0 = clock();
for (int i = 0; i < N; ++i) {
shared_ptr<A> a0(new A());
shared_ptr<A> a1(a0);
A* pa0 = a1.get();
}
clock_t t1 = clock();
for (int i = 0; i < N; ++i) {
shared_ptr<A> a0(new A());
weak_ptr<A> w0(a0);
shared_ptr<A> a1 = w0.lock();
if (a1) {
A* pa0 = a1.get();
}
}
clock_t t2 = clock();
cout << float(t1 - t0) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
cout << float(t2 - t1) / CLOCKS_PER_SEC * 1000 << "ms" << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment