Skip to content

Instantly share code, notes, and snippets.

@nurettin
Last active December 15, 2015 11:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurettin/5254360 to your computer and use it in GitHub Desktop.
Save nurettin/5254360 to your computer and use it in GitHub Desktop.
chokes.
#include <iostream>
#include <string>
#include <boost/range/counting_range.hpp>
typedef boost::iterator_range<boost::counting_iterator<int> > counting_range_int_t;
counting_range_int_t operator "" _ri(char const* wtf, std::size_t len)
{
static std::istringstream parse;
parse.clear();
parse.str(std::string(wtf, wtf+ len));
int begin= 0, end= 0;
parse>> begin;
parse.ignore(2);
parse>> end;
auto range= boost::counting_range(begin, end);
return range;
}
counting_range_int_t operator "" _chokesoncock_ri(char const* wtf, std::size_t len)
{
static std::stringstream parse;
parse.clear();
std::ostream_iterator<char> stupid(parse);
std::copy(wtf, wtf+ len, stupid);
int begin= 0, end= 0;
parse>> begin;
parse.ignore(2);
parse>> end;
auto range= boost::counting_range(begin, end);
return range;
}
#include <iterator>
std::ostream &operator<< (std::ostream &out, counting_range_int_t const &r)
{
out<< '[';
auto end= r.end(); -- end;
std::copy(r.begin(), end, std::ostream_iterator<int>(out, ", "));
return out<< *end<< "]\n";
}
#include <cassert>
#include <boost/date_time.hpp>
namespace pt = boost::posix_time;
struct timeit
{
pt::ptime begin;
timeit(std::string const &msg): begin(pt::microsec_clock::universal_time()){ std::cout<< msg<< std::flush; }
~timeit(){ std::cout<< (pt::microsec_clock::universal_time()- begin).total_milliseconds()<< "ms"<< std::endl; }
};
void testit(std::string const &msg, counting_range_int_t const &r)
{
std::ostringstream testbed;
std::cout<< msg<< std::endl;
testbed<< r;
assert(testbed.str()== "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n");
}
int main()
{
testit("testing _ri", "1..10"_ri);
testit("testing _ri again", "1..10"_ri);
testit("testing _chokesoncock_ri", "1..10"_chokesoncock_ri);
testit("testing _chokesoncock_ri again", "1..10"_chokesoncock_ri);
{
timeit omg("measuring _ri: ");
for(int n= 1000000; n--;)
"10..100"_ri;
}
{
timeit omg("measuring _chokesoncock_ri: ");
for(int n= 1000000; n--;)
"10..100"_chokesoncock_ri;
}
}
/* aaand results are in:
$ g++ -std=c++11 -l boost_date_time test.cpp -O0 && ./a.out
testing _ri
testing _ri again
testing _chokesoncock_ri
testing _chokesoncock_ri again
measuring _ri: 432ms
measuring _chokesoncock_ri: 770ms
$ g++ -std=c++11 -l boost_date_time test.cpp -O1 && ./a.out
testing _ri
testing _ri again
testing _chokesoncock_ri
testing _chokesoncock_ri again
measuring _ri: 313ms
measuring _chokesoncock_ri: 414ms
$ g++ -std=c++11 -l boost_date_time test.cpp -O2 && ./a.out
testing _ri
testing _ri again
testing _chokesoncock_ri
testing _chokesoncock_ri again
measuring _ri: 311ms
measuring _chokesoncock_ri: 405ms
$ g++ -std=c++11 -l boost_date_time test.cpp -O3 && ./a.out
testing _ri
testing _ri again
testing _chokesoncock_ri
testing _chokesoncock_ri again
measuring _ri: 305ms
measuring _chokesoncock_ri: 403ms
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment