why is boost::circular_buffer so slow in my benchmark?
<p>Reading the <a href="http://www.boost.org/doc/libs/1_60_0/doc/html/circular_buffer/rationale.html" rel="nofollow noreferrer">rationale</a>, boost::circular_buffer looked promising:</p>
<blockquote>
<p>Suitability for real-time and performance critical applications.</p>
<p>Fast constant-time insertion and removal of elements from the front and back.</p>
</blockquote>
<p>When I run a simple benchmark that simulates my use case, using it as a byte buffer:</p>
<ol>
<li>write a bigger chunk</li>
<li>read smaller chunks until empty</li>
<li>repeat</li>
</ol>
<p>the performance is absolutely abysmal, more than <a href="http://coliru.stacked-crooked.com/a/72382381b2b1c931" rel="nofollow noreferrer">4000x</a> slower than my own hack and spsc_queue.</p>
<pre><code>lin : 101  // 10240x
lock: 109  // 10240x
circ: 427  // 10x
</code></pre>
<p>Note that the loopcount for <code>circular</code> is <code>10</code> and the loopcount for the others is <code>10*1024</code>. See working example <a href="http://coliru.stacked-crooked.com/a/9bff5f0ec07a20ac" rel="nofollow noreferrer">here</a>.</p>
<p>Am I using it completely wrong or was it just not designed with fundamental / POD types in mind?</p>
<p><strong>Edit:</strong></p>
<p>Adopting the benchmark with the provided changes does not fully resolve the issues on MSVC2015. There a factor of 100x remains.</p>
<pre><code>lin : 69   // 10240x
lock: 79   // 10240x
circ: 9688 // 10240x 
</code></pre>
<p>Inserting several items at once being so slow is problematic. Assigning would work in this special scenario because the buffer is depleted before the insert, but this is not a general solution. In resume spsc_queue wins on all fronts, its fast, can be used without depletion, and can be used in a mulithreaded environment (in a single producer single consumer scenario).</p>