I
Ireneusz SZCZESNIAK
I want to sort a vector with the std::sort function. There are two
functions: one with two arguments, the other with three arguments. I
am using the one with three arguments. I noticed that there is a huge
overhead of using this function, which comes from copying the function
object, i.e., the object that compares the elements of the vector,
which is passed to the function not by reference but by value.
Now, at the end of this mail there is a source code. It's not the
actual code of the problem I have, but only a distilled version of it.
Here the SO class has no fields, so copying an object of this class
is not a big deal. However, in my actual problem the function object
has a very large vector inside, and this function object should not be
copied even once. If you run the program, you'll see the function
object is copied seven times for sorting a vector with three
elements!
So, the problem is that when I use the std::sort function the function
object is copied many times! The larger the sequence to sort, the
greater the number of copies of the function object. It's very bad
for me because I sort small number of elements (up to four), but I do
the sorting many times (millions).
I want to use the sort function in such a way that the function object
is not coppied. I tried to call the sort function this way:
sort<vector<irek *>::iterator, SO &>(v.begin(), v.end(), so);
but it reduced the number of copies by one only.
I am convinced that I am doing something wrong. I would appreciate
your advice on how to solve my problem.
Thanks & best,
Irek
****************************************
Ireneusz SZCZESNIAK - research assistant
http://www.iitis.gliwice.pl/~iszczesniak
****************************************
*********************************************************************
#include <algorithm>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
class irek
{
int i;
public:
irek(int _i) {i = _i;}
int get_i() const {return i;}
};
struct SO : binary_function<irek, irek, bool>
{
public:
SO() {}
SO(const SO&)
{
cout << "coppied!" << endl;
}
result_type operator()(const first_argument_type *a,
const second_argument_type *b)
{
return a->get_i() < b->get_i();
}
};
main()
{
// this is our function object
SO so;
irek a(2), b(3), c(1);
vector<irek *> v;
v.push_back(&b);
v.push_back(&c);
v.push_back(&a);
sort(v.begin(), v.end(), so);
for(int i = 0; i < v.size(); i++)
cout << v->get_i() << endl;
}
functions: one with two arguments, the other with three arguments. I
am using the one with three arguments. I noticed that there is a huge
overhead of using this function, which comes from copying the function
object, i.e., the object that compares the elements of the vector,
which is passed to the function not by reference but by value.
Now, at the end of this mail there is a source code. It's not the
actual code of the problem I have, but only a distilled version of it.
Here the SO class has no fields, so copying an object of this class
is not a big deal. However, in my actual problem the function object
has a very large vector inside, and this function object should not be
copied even once. If you run the program, you'll see the function
object is copied seven times for sorting a vector with three
elements!
So, the problem is that when I use the std::sort function the function
object is copied many times! The larger the sequence to sort, the
greater the number of copies of the function object. It's very bad
for me because I sort small number of elements (up to four), but I do
the sorting many times (millions).
I want to use the sort function in such a way that the function object
is not coppied. I tried to call the sort function this way:
sort<vector<irek *>::iterator, SO &>(v.begin(), v.end(), so);
but it reduced the number of copies by one only.
I am convinced that I am doing something wrong. I would appreciate
your advice on how to solve my problem.
Thanks & best,
Irek
****************************************
Ireneusz SZCZESNIAK - research assistant
http://www.iitis.gliwice.pl/~iszczesniak
****************************************
*********************************************************************
#include <algorithm>
#include <vector>
#include <iostream>
#include <functional>
using namespace std;
class irek
{
int i;
public:
irek(int _i) {i = _i;}
int get_i() const {return i;}
};
struct SO : binary_function<irek, irek, bool>
{
public:
SO() {}
SO(const SO&)
{
cout << "coppied!" << endl;
}
result_type operator()(const first_argument_type *a,
const second_argument_type *b)
{
return a->get_i() < b->get_i();
}
};
main()
{
// this is our function object
SO so;
irek a(2), b(3), c(1);
vector<irek *> v;
v.push_back(&b);
v.push_back(&c);
v.push_back(&a);
sort(v.begin(), v.end(), so);
for(int i = 0; i < v.size(); i++)
cout << v->get_i() << endl;
}