设为首页 加入收藏

TOP

c++ 11学习笔记--Lambda 表达式(对比测试Lambda ,bind,Function Object)(二)
2015-07-20 18:02:47 来源: 作者: 【 】 浏览:11
Tags:学习 笔记 --Lambda 表达式 对比 测试 Lambda bind Function Object
ected:
? ? time_point _starttime;
};
?
bool test_timer()
{
? ? using std::chrono::milliseconds;
? ? typedef timer::duration duration;
? ??
? ? const milliseconds sleep_time(500);
? ??
? ? timer t;
? ? std::this_thread::sleep_for(sleep_time);
? ? duration recorded = t.elapsed();
? ??
? ? // make sure the clock and this_thread::sleep_for is precise within one millisecond (or at least in agreement as to
? ? // how inaccurate they are)
? ? return (recorded - milliseconds(1) < sleep_time)
? ? && (recorded + milliseconds(1) > sleep_time);
}
?
template
void volatile_write(const T& x)
{
? ? volatile T* p = new T;
? ? *p = x;
? ? delete p;
}
?
template
void run_test(const std::string& name, Function func)
{
? ? std::cout << name;
? ? timer t;
? ? volatile_write(func());
? ? timer::duration duration = t.elapsed();
? ? std::cout << '\t' << duration.count() << std::endl;
}
?
template
void do_test_loop(Function func, const uint64_t upper_limit = 100000000ULL)
{
? ? uint64_t i;
? ? for (i = 0; i < upper_limit; ++i)
? ? ? ? func(i);
? ? if(i == upper_limit)
? ? {
? ? ? ? std::cout<
? ? }
}
?
uint64_t test_accumulate_lambda()
{
? ? uint64_t x = 0;
? ? auto accumulator = [&x] (uint64_t i) { x += i;
?};
? ? do_test_loop(accumulator);
? ? return x;
}
?
void test_accumulate_bind_function(uint64_t& x, uint64_t i)
{
? ? x += i;
}
?
uint64_t test_accumulate_bind()
{
? ? namespace arg = std::placeholders;
? ??
? ? uint64_t x = 0;
? ? std::function accumulator = std::bind(&test_accumulate_bind_function, std::ref(x), arg::_1);
? ? do_test_loop(accumulator);
? ? return x;
}
?
uint64_t test_accumulate_bound_lambda()
{
? ? uint64_t x = 0;
? ? std::function accumulator = [&x] (uint64_t i) { x += i; };
? ? do_test_loop(accumulator);
? ? return x;
}
?
?
uint64_t test_accumulate_class_function()
{
? ? uint64_t x = 0;
?
? ? do_test_loop(FunctorClass(x));
? ?// for_each(v.begin(), v.end(), FunctorClass(x));
? ? return x;
}
?
uint64_t test_accumulate_bind_auto()
{
? ? namespace arg = std::placeholders;
? ??
? ? uint64_t x = 0;
? ? auto accumulator = std::bind(&test_accumulate_bind_function, std::ref(x), arg::_1);
? ? do_test_loop(accumulator);
? ? return x;
}
?
#if USE_BOOST
uint64_t test_accumulate_boost_bind()
{
? ? uint64_t x = 0;
? ??
? ? boost::function accumulator = boost::bind(&test_accumulate_bind_function, boost::ref(x), _1);
? ? do_test_loop(accumulator);
? ? return x;
}
?
uint64_t test_accumulate_boost_bound_lambda()
{
? ? uint64_t x = 0;
? ? boost::function accumulator = [&x] (uint64_t i) { x += i; };
? ? do_test_loop(accumulator);
? ? return x;
}
#endif
?
int main()
{
? ? if (!test_timer())
? ? {
? ? ? ? std::cout << "Failed timer test." << std::endl;
? ? ? ? return -1;
? ? }
? ??
? ? run_test("Accumulate (lambda) ? ? ? ? ? ?", &test_accumulate_lambda);
? ? run_test("Accumulate (bind) ? ? ? ? ? ? ?", &test_accumulate_bind);
? ? run_test("Accumulate (bound lambda) ? ? ?", &test_accumulate_bound_lambda);
? ? run_test("Accumulate (Function Object) ? ?", &test_accumulate_class_function);
? ? run_test("Accumulate (bind auto) ? ?", &test_accumulate_bind_auto);
#if USE_BOOST
? ? run_test("Accumulate (boost bind) ? ? ? ?", &test_accumulate_boost_bind);
? ? run_test("Accumulate (boost bound lambda)
首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇hdu 1085 Holding Bin-Laden Capt.. 下一篇HDU 1024:Max Sum Plus Plus

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: