Jump to content
  • Advertisement
Sign in to follow this  
Enigma

Profiling text formatting

This topic is 5155 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is the following a fair test of the various text formatting functions/classes? If not, can anyone suggest a better profiling test? Are there any other alternatives I should include?
int main()
{
	char buffer[32];
	std::string bufferString;
	std::string str[8];
	unsigned int cl[9];
	unsigned int const iterations = 1000000;
	boost::format fmt("%6d");
	cl[0] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		std::sprintf(buffer, "%6d", 132);
		str[0] += buffer;
	}
	cl[1] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		std::snprintf(buffer, 32, "%6d", 132);
		str[1] += buffer;
	}
	cl[2] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		std::ostringstream sstr;
		sstr << std::setw(6) << 132;
		str[2] += sstr.str();
	}
	cl[3] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		std::ostrstream sstr(buffer, 32);
		sstr << std::setw(6) << 132 << std::ends;
		str[3] += buffer;
	}
	cl[4] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		str[4] += boost::lexical_cast< std::string >(132);
	}
	cl[5] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		str[5] += boost::str(boost::format("%6d") % 132);
	}
	cl[6] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		str[6] += boost::str(fmt % 132);
	}
	cl[7] = std::clock();
	for (unsigned int i = 0; i < iterations; ++i)
	{
		formatstream fstr(bufferString);
		fstr << d(132, 6);
		str[7] += bufferString;
	}
	cl[8] = std::clock();
	std::cout << "sprintf:               " << (cl[1] - cl[0]) << " / 1.0\n";
	if (cl[1] == cl[2])
	{
		++cl[1];
	}
	std::cout << "snprintf:              " << (cl[2] - cl[1]) << " / " << (float(cl[2] - cl[1]) / float(cl[1] - cl[0]));
	if (str[1] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "stringstream:          " << (cl[3] - cl[2]) << " / " << (float(cl[3] - cl[2]) / float(cl[1] - cl[0]));
	if (str[2] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "strstream:             " << (cl[4] - cl[3]) << " / " << (float(cl[4] - cl[3]) / float(cl[1] - cl[0]));
	if (str[3] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "lexical_cast:          " << (cl[5] - cl[4]) << " / " << (float(cl[5] - cl[4]) / float(cl[1] - cl[0]));
	if (str[4] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "boost format (local) : " << (cl[6] - cl[5]) << " / " << (float(cl[6] - cl[5]) / float(cl[1] - cl[0]));
	if (str[5] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "boost format (global): " << (cl[7] - cl[6]) << " / " << (float(cl[7] - cl[6]) / float(cl[1] - cl[0]));
	if (str[6] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
	std::cout << "formatstream:          " << (cl[8] - cl[7]) << " / " << (float(cl[8] - cl[7]) / float(cl[1] - cl[0]));
	if (str[7] != str[0])
	{
		std::cout << " FAILED\n";
	}
	else
	{
		std::cout << '\n';
	}
}
Thanks, Enigma

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!