加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 为什么boost :: circular_buffer在我的基准测试中这么慢?

发布时间:2020-12-16 07:15:42 所属栏目:百科 来源:网络整理
导读:阅读 rationale,boost :: circular_buffer看起来很有希望: Suitability for real-time and performance critical applications. Fast constant-time insertion and removal of elements from the front and back. 当我运行一个模拟我的用例的简单基准测试时
阅读 rationale,boost :: circular_buffer看起来很有希望:

Suitability for real-time and performance critical applications.

Fast constant-time insertion and removal of elements from the front and back.

当我运行一个模拟我的用例的简单基准测试时,将其用作字节缓冲区:

>写一个更大的块
>读取较小的块直到空
>重复一遍

性能绝对糟糕,比我自己的hack和spsc_queue慢了超过4000x.

lin : 101  // 10240x
lock: 109  // 10240x
circ: 427  // 10x

请注意,circular的loopcount为10,其他的loopcount为10 * 1024.参见工作示例here.

我使用它是完全错误还是只是没有设计基本/ POD类型?

编辑:

采用提供的更改的基准测试并不能完全解决MSVC2015上的问题.还有100倍的因素.

lin : 69   // 10240x
lock: 79   // 10240x
circ: 9688 // 10240x

一次插入几个项目是如此之慢是有问题的.分配将在此特殊情况下起作用,因为缓冲区在插入之前已耗尽,但这不是一般解决方案.在恢复中,spsc_queue在所有前端获胜,其快速,可以在不耗尽的情况下使用,并且可以在多线程环境中使用(在单个生产者单个消费者场景中).

解决方法

首先,确保基准测试是合理的.如果您不使用计算结果,编译器会在您最不期望的时候将其作为死代码消除.

>你的循环删除看起来不是最理想的.请改用:

buffer.erase_begin(1024); // or indeed,use checked size see below

UPDATE

>第二件影响性能的事情 – 严重 – 是插入调用.在你的用例中,你可以使用assign,就像在竞争者中一样,被编译成mempcy或memmove.
>确保禁用调试(定义NDEBUG和/或BOOST_CB_DISABLE_DEBUG)

这是我使用Nonius:http://paste.ubuntu.com/15222217/的重构基准

时钟分辨率:平均值为18.6412 ns(40960002次迭代)

benchmarking linear
collecting 100 samples,1 iterations each,in estimated 3.93727 s
mean: 39.0804 ms,lb 39.0567 ms,ub 39.1051 ms,ci 0.95
std dev: 124.19 μs,lb 111.153 μs,ub 141.079 μs,ci 0.95
found 0 outliers among 100 samples (0%)
variance is unaffected by outliers

benchmarking lockfree
collecting 100 samples,in estimated 4.78513 s
mean: 37.0188 ms,lb 37.0106 ms,ub 37.0277 ms,ci 0.95
std dev: 43.5788 μs,lb 37.3685 μs,ub 52.8458 μs,ci 0.95
found 3 outliers among 100 samples (3%)
variance is unaffected by outliers

benchmarking circular
collecting 100 samples,in estimated 9.78763 s
mean: 62.884 ms,lb 62.8657 ms,ub 62.9041 ms,ci 0.95
std dev: 98.0325 μs,lb 85.6543 μs,ub 119.395 μs,ci 0.95
found 1 outliers among 100 samples (1%)
variance is unaffected by outliers

互动结果:http://stackoverflow-sehe.s3.amazonaws.com/57c2bfea-3e9d-4503-8d23-3b88209fc3ce/stats.html

enter image description here

没有nonius:Live On Coliru

产量

lin : 101 (checksum: -1741910392)
lock: 89 (checksum: -1741910392)
circ: 102 (checksum: -1741910392)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读