1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <iostream>
#include <string>
#include <memory>
#include <chrono>
#include <vector>
using namespace std;
class Print{
public:
virtual void print(int p1, float p2, float p3, float p4){}
};
class PrintWrapper
{
public:
PrintWrapper(shared_ptr<Print> print, int p1, float p2, float p3, float p4) :
m_print(print), _p1(p1),_p2(p2),_p3(p3),_p4(p4){}
~PrintWrapper(){}
void execute()
{
m_print->print(_p1,_p2,_p3,_p4);
}
private:
shared_ptr<Print> m_print;
int _p1;
float _p2,_p3,_p4;
};
void test()
{
shared_ptr<Print> p(new Print());
shared_ptr<PrintWrapper> pw(new PrintWrapper(p, 1, 2.f,3.0f,4.0f));
//-------------test 1-------------------------
auto time1 = std::chrono::system_clock::now();
for (auto var = 0; var < 1000000; ++var)
{
p->print(1, 2.f,3.0f,4.0f);
}
auto time2 = std::chrono::system_clock::now();
cout <<"test 1 : "<< (time2 - time1).count() << " microseconds." << endl;
//-------------test 2-------------------------
auto time3 = std::chrono::system_clock::now();
for (auto var = 0; var < 1000000; ++var)
{
pw->execute();
}
auto time4 = std::chrono::system_clock::now();
cout <<"test 2 : "<< (time4 - time3).count() << " microseconds." << endl;
}
int main() { test(); } |
Partager