Hmm I happen to use the idea of replacing begin with rbegin and iterators with reverse_iterator, etc Doing this I wrote a simple test file to compare performance. To my surprise doing std::reverse(v.begin(), v.end()) and than using BOOST_FOREACH is actually faster than a hard brute force method of BOOST_FOREACH_REV.
Test ran with mingw compiler 3.4.5
Heres the code.
#include <vector>
#include <iostream>
#include <algorithm>
#include <boost/timer.hpp>
#include <boost/foreach.hpp>
#include <boost/format.hpp>
int main()
{
const long TOTAL_ELEMENT = 10000;
std::cout << boost::format("%f\t%f\n") % "BOOST_FOREACH" % "BOOST_FOREACH_REV";
boost::timer t;
std::vector<int> vec_int;
for (int j = 1; j < 50; ++j)
{
vec_int.reserve(TOTAL_ELEMENT*j);
double t1, t2;
for (int i = 0; i < TOTAL_ELEMENT*j; ++i)
{
vec_int.push_back(i);
}
t.restart();
std::reverse(vec_int.begin(), vec_int.end());
BOOST_FOREACH(int& i, vec_int)
{
i -= 1;
}
t1 = t.elapsed();
t.restart();
BOOST_FOREACH_REV(int& i, vec_int)
{
i -= 1;
}
t2 = t.elapsed();
std::cout << boost::format("%f\t%f\n") % t1 % t2;
}
}
/*
BOOST_FOREACH BOOST_FOREACH_REV
0.001000 0.001000
0.002000 0.003000
0.002000 0.005000
0.004000 0.005000
0.005000 0.006000
0.003000 0.009000
0.010000
0.008000
0.008000 0.010000
0.008000 0.010000
0.008000 0.014000
0.009000 0.016000
0.016000 0.014000
0.010000 0.019000
0.011000 0.017000
0.013000 0.017000
0.013000 0.018000
0.014000 0.022000
0.016000 0.020000
0.016000 0.021000
0.017000 0.023000
0.017000 0.025000
0.018000 0.031000
0.018000 0.026000
0.020000 0.034000
0.019000 0.030000
0.022000
0.037000
0.021000 0.033000
0.024000 0.032000
0.023000 0.039000
0.025000 0.033000
0.026000 0.039000
0.026000 0.046000
0.029000 0.040000
0.028000 0.039000
0.030000 0.042000
0.030000 0.051000
0.032000 0.051000
0.035000 0.059000
0.035000 0.054000
0.033000 0.055000
0.035000 0.059000
0.035000 0.119000
0.035000 0.062000
0.037000 0.059000
0.037000
0.057000
0.037000 0.063000
0.072000 0.061000
0.039000 0.062000
0.039000 0.060000
*/
HI, yeah i wanted to do reverse iteration lookup so I took liberty to edit the file of foreach.hpp. Its ugly but it "works". I haven't done any detail testing.
I only test it on basic string.. so i am sure of that.On 7/1/07, Pete Bartlett < pete@pcbartlett.com> wrote:
>You're not the first to want a REVERSE_FOREACH, but this is the first
>legitimate argument I've heard for it. Could you go to
> http://svn.boost.org/trac and open a new ticket there. Make the ticket
>type "feature request" and the component "foreach". I'll look into it
>when I have a chance.
Thanks very much for your interest, Eric.
I've set up a ticket on the issue which you can view at
http://svn.boost.org/trac/boost/ticket/1071
Pete Bartlett
_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users