Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <memory>
- #include <time.h>
- #include <stdio.h>
- using namespace std;
- struct IntValue {
- int x;
- IntValue(int x) : x(x) {}
- };
- class MyClass_Ptr {
- unique_ptr<IntValue> a, b, c;
- public:
- MyClass_Ptr(int s)
- : a(new IntValue(s))
- , b(new IntValue(0))
- , c(new IntValue(0))
- {}
- void Compute() {
- a->x += b->x + c->x;
- b->x += a->x + c->x;
- c->x += a->x + b->x;
- }
- void Print() {
- printf("%d\n", a->x + b->x + c->x);
- }
- };
- class MyClass_Memb {
- IntValue a, b, c;
- public:
- MyClass_Memb(int s)
- : a(s)
- , b(0)
- , c(0)
- {}
- void Compute() {
- a.x += b.x + c.x;
- b.x += a.x + c.x;
- c.x += a.x + b.x;
- }
- void Print() {
- printf("%d\n", a.x + b.x + c.x);
- }
- };
- int main() {
- {
- unique_ptr<MyClass_Memb> obj(new MyClass_Memb(1));
- int start = clock();
- for (int i = 0; i < 1000000000; i++)
- obj->Compute();
- obj->Print();
- printf("%0.3lf\n", double(clock() - start) / CLOCKS_PER_SEC);
- }
- {
- unique_ptr<MyClass_Ptr> obj(new MyClass_Ptr(1));
- int start = clock();
- for (int i = 0; i < 1000000000; i++)
- obj->Compute();
- obj->Print();
- printf("%0.3lf\n", double(clock() - start) / CLOCKS_PER_SEC);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement