‘boost::bind를 맘 내키는 대로 써도 될까?’라는 의문에 각 경우의 속도를 비교해보기로 했다.
우선, 재물로 사용될 클래스를 만들고…
class test_class
{
public:
test_class()
: _i(10)
{}
void show(int i)
{
++_i;
}
private:
int _i;
};// class test_class
아래는 테스트코드의 일부. TIME_DURATION은 포함되는 블록의 수행시간을 마이크로 초단위로 돌려준다.
const int repeat_num = 100000;
hs_int64 d;
test_class t;
TIME_DURATION(d)
{
for (int i = 0; i < repeat_num; ++i)
t.show(i);
}
cout << "direct call: " << d << endl;
TIME_DURATION(d)
{
for (int i = 0; i < repeat_num; ++i)
boost::bind(&test_class::show, &t, i)();
}
cout << "boost.bind call: " << d << endl;
boost::_bi::bind_t, boost::_bi::list2, boost::arg<1> > > t_mem_boost = boost::bind(&test_class::show, &t, _1);
TIME_DURATION(d)
{
for (int i = 0; i < repeat_num; ++i)
t_mem_boost(i);
}
cout << "boost.bind call via instance: " << d << endl;
TIME_DURATION(d)
{
for (int i = 0; i < repeat_num; ++i)
std::mem_fun(&test_class::show)(&t, i);
}
cout << "mem_fun call: " << d << endl;
std::mem_fun1_t t_mem_stl = std::mem_fun(&test_class::show);
TIME_DURATION(d)
{
for (int i = 0; i < repeat_num; ++i)
t_mem_stl(&t, i);
}
cout << "mem_fun call via instance: " << d << endl;
결과
direct call: 658 boost.bind call: 9152 boost.bind call via instance: 4034 mem_fun call: 1972 mem_fun call via instance: 950
테스트를 반복해도 결과는 대동소이하다. boost::bind는 직접호출보다 10배 이상 느리다. 그나마 mem_fun이 좀 낮다.
따라서…
‘boost::bind를 맘 내키는 대로 써도 될까?’ -> 살살 쓰자