[ create a new paste ] login | about

Link: http://codepad.org/ib98T2Gd    [ raw code | output | fork ]

C++, pasted on Dec 23:
#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;
}


Output:
1
2
3
convert int 12345678 to std::string 100000 times. 
f1(): 0.02 sec
f2(): 0.07 sec


Create a new paste based on this one


Comments: