I don't know the inner workings of either boost::function or
std::function. It's not boost's fault per se, but there are a couple
of things you could do differently.
> int func2(std::vector<int> i){
> total += i.size();
> return i.size();
> }
Pass a reference or even a pointer instead of the whole vector. You
are copying the vector every time.
> const int T = 1000000;
> s = boost::chrono::system_clock::now();
> for (int i = 0; i < T; ++i)
> call(boost::bind(&func2, v));
> e = boost::chrono::system_clock::now();
You are also binding func2 every time; not sure if that's getting
optimized or not.
You likely want to bind with a placeholder instead of the vector
itself, then call the binding itself. i.e.
auto binding = boost::bind(&func2, _1);
binding(v);
I do that frequently enough; and with more event-driven systems,
boost::signals, etc, it is unavoidable. You want the placeholder
instead, once-bound/later-called.
> In case you need to know my enviorment, my OS is Arch, compiler is gcc
> 4.9.0, and optimizations are default.
Default can mean a lot of things. Debug or release mode? Beyond those
two broad categories, do verify your settings and build for release
mode.
HTH
> The execution time (ms) of three versions I tried:
> boost::function with C++11 : 173874
> boost::function without C++11 : 3676
> std::function in C++11 : 29233
>
> Any thoughts are appreciated!
> Thanks,
> Athrun
>
>
>
> _______________________________________________
> Boost-users mailing list
> Boost...@lists.boost.org
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Well, you're testing potentially two things: STL performance, or
std::bind performance. Couldn't tell you which is the bottleneck, but
neither in my mind are trivial depending how many elements are in your
vector.
Hi!
I'd run the example code through Callgrind (valgrind --tool=callgrind)
and then compare the reports in KCachegrind. This should at least give
you an idea of the source of the problem.
I may do that myself during the weekend in case you would not able to do
that on your own.
WBR,
Adam Romanek
I wouldn't swear off boost::bind or even std::bind. It has its place.
But like I explained in an earlier response, bind is something you
(you, generally) generally want to do once, like when you are
connecting signal slots, wiring up functions, and so on. Once you have
the thing bound, leave it alone. The same is true for any functor,
type thing; there is always overhead instantiating a class (or
struct), so generally you want to pick up the instance(s) you
need/want, then work with them forever and ever, Amen.
>>> Maybe trying another compiler would help you obtain your "proof".
>>> How about intel compiler?
>>> It also has the "-std=c++11" flag, so, if it is a compiler issue it will
>>> be clear.
> As you suggested, I tried Intel compiler. It's great. The data shows there
> is no performance differences between with or without C++11 in
> boost::function. That proofs your guess that it's a compiler issue.
>
> So here is my conclusion, there won't be memory copy optimization when using
> (C++11, boost::function, gcc4.9/clang3.4). But Intel compiler does well to
> do C++11 related optimizations.
Glad to have helped some.
Hi, there,Thanks for you guys help. I think I have reached the answer I wanted. So I'd like to explain why there are performance issues with C++11 in boost::function.First of all, I wanna reply the guys who give me hints.>> What if you try with just 1 integer? Does the difference in time stay proportionally the same?There won't be big differences if there is just 1 integer. So this proofs that it could be a memory copy problem.>> I'd run the example code through Callgrind (valgrind --tool=callgrind)>> and then compare the reports in KCachegrind. This should at least give>> you an idea of the source of the problem.You are right. I ran callgrind, and it showed some interesting things. If I compiled without C++11, it shows the most hot spot is "wordcopy_fwd_align". Otherwise, the most hotspot is "boost::bind" and there is no records for wordcopy_fwd_align. I think that means if we dont' use C++11, there will be some memory copy optimizations.>> Maybe trying another compiler would help you obtain your "proof".>> How about intel compiler?>> It also has the "-std=c++11" flag, so, if it is a compiler issue it will be clear.As you suggested, I tried Intel compiler. It's great. The data shows there is no performance differences between with or without C++11 in boost::function. That proofs your guess that it's a compiler issue.So here is my conclusion, there won't be memory copy optimization when using (C++11, boost::function, gcc4.9/clang3.4). But Intel compiler does well to do C++11 related optimizations.
As promised I performed a simple test during the weekend and wasn't able
to reproduce the issue. See the code below:
---
#include <vector>
#include <boost/bind.hpp>
#include <boost/function.hpp>
void call(boost::function<int ()> f) {
f();
}
long long total = 0;
int func2(std::vector<int> i){
total += i.size();
return i.size();
}
int main() {
std::vector<int> v(100);
const int T = 1000000;
// s = boost::chrono::system_clock::now();
for (int i = 0; i < T; ++i)
call(boost::bind(&func2, v));
// e = boost::chrono::system_clock::now();
}
---
The performance does not change when compiling with -std=c++11 or
without it. I compile the code like this:
$ g++ -I/home/A.Romanek/tmp/boost/boost_1_55_0 main.cpp -std=c++11 -O2
&& time ./a.out
real 0m1.669s
My setup is Ubuntu 14.04, gcc 4.8.2 and my CPU is Intel Core2Duo P8700 @
2.53GHz.
Could you please provide a complete example so that I could reproduce
the issue on my desk?