复制std :: function有多昂贵? [英] How expensive is to copy an std::function?

查看:318
本文介绍了复制std :: function有多昂贵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: function 是可移动的,在某些情况下这是不可能的或不方便的。

While std::function is movable, in some cases it's not possible or not convenient. Is there a significant penalty for copying it?

它是否可能取决于捕获的变量的大小(如果它是使用lambda表达式创建的)?

Does it possibly depend on the size of the captured variables (if it was created using a lambda expression)? Is it implementation dependent?

推荐答案

它确实取决于实现。它还取决于您在 std :: function 中存储的内容。当然,它不比简单地复制C样式的函数指针便宜。

It is indeed implementation-dependent. It also depends on what exactly you are storing in the std::function. It is surely not as cheap as simply copying a C-style function pointer.

最好的办法是让您尝试以清晰方便的方式编写代码,然后,如果运行速度不够快,请对其进行分析。如果需要最终的性能,可能会发现 std :: function 根本不合适。

The best thing would be for you to try writing the code the way you find clear and convenient, then, if it does not run quickly enough, profile it. If you need ultimate performance, you may find std::function is not suitable at all.

这篇关于复制std :: function有多昂贵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆