I have performed some test based on information that I read in http://ift.tt/25PeofA
The purpose of measurement was to check how much time consuming is single allocation and double allocation (bad style) of shared_ptr. I assume that single allocation ought to be less time consuming than double allocation. So I would like to know if I misunderstood sth or memory allocation has no correlation with time.
The test code:
#include <iostream>
#include <memory>
#include <chrono>
#include <string>
using namespace std;
class Test{
private:
int value;
string name;
double value2;
public:
Test(int v, string n, double v2) : value(v), name(n), value2(v2){}
~Test(){}
};
void singleAllocation(){
chrono::system_clock::time_point start = chrono::system_clock::now();
for(int i = 0; i < 3000; i++)
shared_ptr<Test> sp(make_shared<Test>(10, "This is simple test", 2.3334));
chrono::system_clock::time_point end = chrono::system_clock::now();
cout<<"single allocation of 3000 objects took "
<<chrono::duration_cast<chrono::microseconds>(end - start).count()
<<"us.\n";
}
void doubleAllocation(){
chrono::system_clock::time_point start = chrono::system_clock::now();
for(int i = 0; i < 3000; i++)
shared_ptr<Test> sp(new Test(10, "This is simple test", 2.3334));
correlaction
chrono::system_clock::time_point end = chrono::system_clock::now();
cout<<"\n\ndouble allocation of 3000 objects took "
<<chrono::duration_cast<chrono::microseconds>(end - start).count()
<<"us.\n";
}
int main(){
singleAllocation();
doubleAllocation();
}
The output: single allocation of 3000 objects took 2483us.
double allocation of 3000 objects took 1226us.
Aucun commentaire:
Enregistrer un commentaire