#include <iostream>
#include <boost/lexical_cast.hpp>
#include <boost/timer.hpp>
std::string f1(int n)
{
char buf[16];
std::string str;
sprintf(buf, "%d", n);
str += buf;
return str;
}
std::string f2(int n)
{
std::string str;
str = boost::lexical_cast<std::string>(n);
return str;
}
int main(int argc, char* argv[])
{
int c = 100000;
if(argc>1)
{
c = boost::lexical_cast<int>(argv[1]);
}
int n = 12345678;
if(argc>2)
{
n = boost::lexical_cast<int>(argv[2]);
}
std::cout << "convert int " << n << " to std::string " << c << " times. " << std::endl;
boost::timer t1;
for(int i=0; i<c; ++i)
{
f1(n);
}
std::cout << "f1(): " << t1.elapsed() << " sec" << std::endl;
boost::timer t2;
for(int i=0; i<c; ++i)
{
f2(n);
}
std::cout << "f2(): " << t2.elapsed() << " sec" << std::endl;
return 0;
}