I need to find the max element in the vector so I'm using std::max_element
, but I've found that it's a very slow function, so I wrote my own version and manage to get x3 better performance, here is the code:
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <sys/time.h>
double getRealTime()
{
struct timeval tv;
gettimeofday(&tv, 0);
return (double) tv.tv_sec + 1.0e-6 * (double) tv.tv_usec;
}
inline int my_max_element(const std::vector<int> &vec, int size)
{
auto it = vec.begin();
int max = *it++;
for (; it != vec.end(); it++)
{
if (*it > max)
{
max = *it;
}
}
return max;
}
int main()
{
const int size = 1 << 20;
std::vector<int> vec;
for (int i = 0; i < size; i++)
{
if (i == 59)
{
vec.push_back(1000000012);
}
else
{
vec.push_back(i);
}
}
double startTime = getRealTime();
int maxIter = *std::max_element(vec.begin(), vec.end());
double stopTime = getRealTime();
double totalIteratorTime = stopTime - startTime;
startTime = getRealTime();
int maxArray = my_max_element(vec, size);
stopTime = getRealTime();
double totalArrayTime = stopTime - startTime;
std::cout << "MaxIter = " << maxIter << std::endl;
std::cout << "MaxArray = " << maxArray << std::endl;
std::cout << "Total CPU time iterator = " << totalIteratorTime << std::endl;
std::cout << "Total CPU time array = " << totalArrayTime << std::endl;
std::cout << "iter/array ratio: = " << totalIteratorTime / totalArrayTime << std::endl;
return 0;
}
Output:
MaxIter = 1000000012
MaxArray = 1000000012
Total CPU time iterator = 0.000989199
Total CPU time array = 0.000293016
iter/array ratio: = 3.37592
on average std::max_element
takes x3 more time then my_max_element
.
So why am I able to create a much faster std function so easily? Should I stop using std and write my own functions since std is so slow?
Note: at first I though it was because I'm using and integer i
in a for loop instead of an iterator, but that seams to not matter now.
Compiling info:
g++ (GCC) 4.8.2
g++ -O3 -Wall -c -fmessage-length=0 -std=c++0x
g++ -Wall -O2
if using GCC...)? – Thaddeusthaddus-pthread
makes them closer – Li-DNDEBUG
to your command line. Maybe there are assertions inside the standard library. – Conradmy_max_element
breaks on empty vectors whereasstd::max_element
is required to detect and handle that case – PeterkinNDEBUG
andmy_max_element
beforestd::max_element
.iter/array ratio: = 0.995192
which is a very marginal win for the standard library. – Forage-O2
the difference disappears (and both time align to the time ofstd::max_element
at-O3
); it's the optimizer that is pulling some strange tricks. – Incisorassert
anywhere,NDEBUG
won't affect it – Sansculotte