Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::function performance issues with C++11
From: Adam Romanek (romanek.adam_at_[hidden])
Date: 2014-07-08 06:11:37


On 05.07.2014 01:23, 饭桶 wrote:
> Cool, that maybe helpful. I will try it and see if I can identify any
> thing interestring here.
>
> Thanks
> Athrun
>
>
> At 2014-07-04 08:10:46, "Adam Romanek" <romanek.adam_at_[hidden]> wrote:
>>On 03.07.2014 23:10, 饭桶 wrote:
>>> Hi,
>>>
>>> Does anyone know why -std=c++11 causes so much difference on
>>> boost::function?
>>>
>>> I was planed to understand if there any performance issues with big size
>>> of parameters.
>>> So I wrote a function that takes a vector as parameter, like func2
>>> shows. I know it's better to use a pointer or reference as function
>>> parameter. I just want to evaluate the performance, so let's keep the
>>> vector as parameter.
>>>
>>> However, I found that it's quite slow when compiled with -std=c++11. In
>>> detail, it takes 173874 milliseconds with C++11, while it takes 3676
>>> seconds without C++11.
>>>
>>> About 50 times slower!! How can that be?
>>>
>>> In my opinion, I thought boost::function should had the same performance
>>> with std::function. So I decided to try std::function in C++11 directly,
>>> Finally, it takes about 29233 milliseconds. That's till 8 times slows!
>>>
>>> Can anyone tell me what happend here?
>>>
>>> int func2(std::vector<int> i){
>>> total += i.size();
>>> return i.size();
>>> }
>>>
>>> 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();
>>>
>>> In case you need to know my enviorment, my OS is Arch, compiler is gcc
>>> 4.9.0, and optimizations are default.
>>> 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!
>>
>>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.

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?
WBR,
Adam Romanek

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net