I made a small test to check the performance of global function/functor/lambda as comparator parameters for std::sort
function. Functor and lambda give the same performance. I was surprised to see, that global function, which appears to be the simplest callback, is much slower.
#include <stdafx.h>
#include <windows.h>
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
const int vector_size = 100000;
bool CompareFunction(const string& s1, const string& s2)
{
return s1[0] < s2[0]; // I know that is crashes on empty string, but this is not the point here
}
struct CompareFunctor
{
bool operator() (const string& s1, const string& s2)
{
return s1[0] < s2[0];
}
} compareFunctor;
int main()
{
srand ((unsigned int)time(NULL));
vector<string> v(vector_size);
for(size_t i = 0; i < vector_size; ++i)
{
ostringstream s;
s << rand();
v[i] = s.str().c_str();
}
LARGE_INTEGER freq;
LARGE_INTEGER beginTime, endTime;
QueryPerformanceFrequency(&freq);
QueryPerformanceCounter(&beginTime);
// One of three following lines should be uncommented
sort(v.begin(), v.end(), CompareFunction);
// sort(v.begin(), v.end(), compareFunctor);
// sort(v.begin(), v.end(), [](const string& s1, const string& s2){return s1[0] < s2[0];});
QueryPerformanceCounter(&endTime);
float f = (endTime.QuadPart - beginTime.QuadPart) * 1000.0f/freq.QuadPart; // time in ms
cout << f << endl;
return 0;
}
A bit of Windows-specific code is used for precise execution time measurement. Environment: Windows 7, Visual C++ 2010. Of course, Release configuration with default optimizations turned on. Execution time:
Global function 2.6 - 3.6 ms (???)
Functor - 1.7 - 2.4 ms
Lambda - 1.7 - 2.4 ms
So, why the global function is slower? Some problem with VC++ compiler, or something else?