Boost C++ Libraries: Ticket #8676: Order of transformed and sliced makes difference https://svn.boost.org/trac10/ticket/8676 <p> See issue reported here: <a class="ext-link" href="http://comments.gmane.org/gmane.comp.lib.boost.devel/242170"><span class="icon">​</span>http://comments.gmane.org/gmane.comp.lib.boost.devel/242170</a> </p> <p> Suppose you wish to transform and slice a random access range. Surprisingly the order in which you apply the operations makes a big difference to performance. I'm guessing this is not desired behaviour, correct me if I'm wrong. AFAICT when transforming happens before slicing the iterators involved in the internals of boost.range are being treated as forward iterators not random access iterators. An advance() operation is made on one of the iterators with a negative number and this advance is O(n) for forward iterators but O(1) for random access. The code below demonstrates (adapted from sliced example code). </p> <p> On an unrelated note the documentation for transformed does not mention that the function is part of the range return type. It is given as: boost::transformed_range&lt;typeof(rng)&gt; </p> <p> Code to demonstrate the transform then slice problem: </p> <pre class="wiki">#include &lt;boost/range/adaptor/transformed.hpp&gt; #include &lt;boost/range/adaptor/sliced.hpp&gt; #include &lt;boost/range/algorithm/copy.hpp&gt; #include &lt;boost/assign.hpp&gt; #include &lt;iterator&gt; #include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;functional&gt; struct identity { typedef int result_type; result_type operator()( int i ) const { return i; } }; int main(int argc, const char* argv[]) { using namespace boost::adaptors; using namespace boost::assign; std::vector&lt; int &gt; input; input += 1,2,3,4,5,6,7,8,9; // slicing then transforming my iterator range std::cout &lt;&lt; "Sliced then transformed: "; boost::copy( input | sliced( 2, 8 ) | transformed( identity() ), std::ostream_iterator&lt; int &gt;( std::cout, ",") ); std::cout &lt;&lt; "\n"; // transforming then slicing my iterator range - takes a very long time.... std::cout &lt;&lt; "Transformed then sliced: "; boost::copy( input | transformed( identity() ) | sliced( 2, 8 ), std::ostream_iterator&lt; int &gt;(std::cout, ",")); std::cout &lt;&lt; "\n"; return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8676 Trac 1.4.3 Neil Groves Wed, 26 Feb 2014 22:13:02 GMT status changed https://svn.boost.org/trac10/ticket/8676#comment:1 https://svn.boost.org/trac10/ticket/8676#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket Neil Groves Mon, 03 Mar 2014 00:11:18 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8676#comment:2 https://svn.boost.org/trac10/ticket/8676#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket