V
Victor Bazarov
Hello,
You might find this interesting/useful...
Using a temporary object as a class predicate with 'std::sort' turned
out to be faster than using a function pointer. Check it out:
------------------------------------------------------------------ >8
#include <iostream>
#include <ostream>
#include <time.h>
#include <algorithm>
struct S
{
int i;
double d;
S(int i_ = 0, double d_ = 0) : i(i_), d(d_) {}
};
bool compareFunc(S const &s1, S const &s2)
{
return s1.i < s2.i || (s1.i == s2.i && s1.d < s2.d);
}
struct compareFunctor
{
bool operator()(S const &s1, S const &s2) const
{
return s1.i < s2.i || (s1.i == s2.i && s1.d < s2.d);
}
};
int main()
{
const int MANY = 50000000;
S *sArr = new S[MANY];
clock_t c0 = clock();
std::sort(sArr, sArr + MANY, compareFunc);
clock_t c1 = clock();
std::sort(sArr, sArr + MANY, compareFunctor());
clock_t c2 = clock();
std::cout << "std::sort with a function as a predicate "
<< c1 - c0 << std::endl;
std::cout << "std::sort with a functor for a predicate "
<< c2 - c1 << std::endl;
std::cout << "Once more...";
clock_t c10 = clock();
std::sort(sArr, sArr + MANY, compareFunctor());
clock_t c11 = clock();
std::sort(sArr, sArr + MANY, compareFunc);
clock_t c12 = clock();
std::cout << std::endl;
std::cout << "std::sort with a function as a predicate "
<< c12 - c11 << std::endl;
std::cout << "std::sort with a functor for a predicate "
<< c11 - c10 << std::endl;
delete[] sArr;
}
------------------------------------------------------------------ >8
Results (on my machine):
std::sort with a function as a predicate 375
std::sort with a functor for a predicate 264
Once more...
std::sort with a function as a predicate 375
std::sort with a functor for a predicate 261
My guess is that the compiler I used (VC9) manages to inline the functor
code (a member function operator()) better than it can a stand-alone
function when they are used as the predicate of 'std::sort'.
I am sure I'm not the first one to find this, I just hadn't run across
that behaviour until a few days ago.
Do criticize it, of course.
V
You might find this interesting/useful...
Using a temporary object as a class predicate with 'std::sort' turned
out to be faster than using a function pointer. Check it out:
------------------------------------------------------------------ >8
#include <iostream>
#include <ostream>
#include <time.h>
#include <algorithm>
struct S
{
int i;
double d;
S(int i_ = 0, double d_ = 0) : i(i_), d(d_) {}
};
bool compareFunc(S const &s1, S const &s2)
{
return s1.i < s2.i || (s1.i == s2.i && s1.d < s2.d);
}
struct compareFunctor
{
bool operator()(S const &s1, S const &s2) const
{
return s1.i < s2.i || (s1.i == s2.i && s1.d < s2.d);
}
};
int main()
{
const int MANY = 50000000;
S *sArr = new S[MANY];
clock_t c0 = clock();
std::sort(sArr, sArr + MANY, compareFunc);
clock_t c1 = clock();
std::sort(sArr, sArr + MANY, compareFunctor());
clock_t c2 = clock();
std::cout << "std::sort with a function as a predicate "
<< c1 - c0 << std::endl;
std::cout << "std::sort with a functor for a predicate "
<< c2 - c1 << std::endl;
std::cout << "Once more...";
clock_t c10 = clock();
std::sort(sArr, sArr + MANY, compareFunctor());
clock_t c11 = clock();
std::sort(sArr, sArr + MANY, compareFunc);
clock_t c12 = clock();
std::cout << std::endl;
std::cout << "std::sort with a function as a predicate "
<< c12 - c11 << std::endl;
std::cout << "std::sort with a functor for a predicate "
<< c11 - c10 << std::endl;
delete[] sArr;
}
------------------------------------------------------------------ >8
Results (on my machine):
std::sort with a function as a predicate 375
std::sort with a functor for a predicate 264
Once more...
std::sort with a function as a predicate 375
std::sort with a functor for a predicate 261
My guess is that the compiler I used (VC9) manages to inline the functor
code (a member function operator()) better than it can a stand-alone
function when they are used as the predicate of 'std::sort'.
I am sure I'm not the first one to find this, I just hadn't run across
that behaviour until a few days ago.
Do criticize it, of course.
V