Lesson learned, always use optimizations when doing benchmarks...
I decided to look at std::unique_ptr
as an alternative for my program. The reasons as to why are not important.
After using compiler optimizations, they seems to take equivalent amounts of time.
How I tested:
time_t current_time;
time(¤t_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 64 bit
- MSVC compiler in Visual Studio 2013. Using Release.
- Intel 4790k (standard clock)
- 1600 MHz RAM
My results (using optimized compilation):
// NUM_TESTS = 1,000,000
/*
STD:
0.001005
0.001001
0.001000
0.001000
0.001015
*/
/*
unique_ptr:
0.001000
0.001000
0.000997
0.001000
0.001017
*/