vendredi 6 mars 2015

why is std::unique_ptr much slower than standard pointer

I decided to look at std::unique_ptr as an alternative for my program, the reasons as to why are not important. My question is why they are so much slower than using a standard pointer in the case I will preset.


How I tested:



time_t current_time;
time(&current_time);
srand((int)current_time);
//int* p0 = new int[NUM_TESTS];
//int* p1 = new int[NUM_TESTS];
std::unique_ptr<int[]> u_p0{ new int[NUM_TESTS] };
std::unique_ptr<int[]> u_p1{ new int[NUM_TESTS] };
for (unsigned i = 0; i < NUM_TESTS; ++i){
u_p0[i] = rand(); // use p0 and p1 for the standard ptr test
u_p1[i] = rand();
}
int result;
auto start = std::chrono::steady_clock::now();
for (unsigned index = 0; index < NUM_TESTS; ++index){
result = u_p0[index] + u_p1[index]; // use p0 and p1 for standard ptr test
}
auto end = std::chrono::steady_clock::now();
double duration = std::chrono::duration_cast<std::chrono::duration<double>>(end - start).count();
printf("time: %f\n", duration);


My Environment:



  • Windows 8.1 64bit

  • MSVC compiler in VS2013 (standard flags)

  • Intel 4790k (standard clock)

  • 1600MHz RAM


My Results:



// NUM_TESTS = 1,000,000

// Take 1
/*
STD:
0.002000
0.002011
0.002001
0.002001
0.002001
*/
/*
unique_ptr:
0.003001
0.004001
0.002985
0.004001
0.004021
*/

// Take 2
/*
STD:
0.003016
0.002015
0.002001
0.002001
0.002000
*/
/*
unique_ptr:
0.005011
0.004011
0.004001
0.004001
0.003001
*/

Aucun commentaire:

Enregistrer un commentaire