std::function 的性能开销是多少?

2021-12-24 00:00:00 c++ boost std

我在论坛上听说使用 std::function<> 会导致性能下降.这是真的吗?如果是真的,是不是性能下降很大?

I heard on a forum using std::function<> causes performance drop. Is it true? If true, is it a big performance drop?

推荐答案

您可以从 boost 的参考资料中找到信息:通过 boost::function 调用会产生多少开销? 和 性能

You can find information from the boost's reference materials: How much overhead does a call through boost::function incur? and Performance

这并不能确定增强功能的是或否".鉴于程序的要求,性能下降可能是可以接受的.通常情况下,程序的某些部分不是性能关键的.即便如此,它也可能是可以接受的.这只能由您来确定.

This doesn't determine "yes or no" to boost function. The performance drop may be well acceptable given program's requirements. More often than not, parts of a program are not performance-critical. And even then it may be acceptable. This is only something you can determine.

对于标准库版本,标准只定义了一个接口.使其工作完全取决于个人实现.我想会使用与 boost 函数类似的实现.

As to the standard library version, the standard only defines an interface. It is entirely up to individual implementations to make it work. I suppose a similar implementation to boost's function would be used.

相关文章