This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Strange optimization results


I have tested performance of std::string compare methods and get
strange results. With -O2 optimization std::string::compare 30 times
faster then operator==. With -Os optimization operator== 5 times
faster then std::string::compare. Is this bug or feature?

Full table (time for 10000000 comparisons in seconds):
			-O0		-O1		-O2		-O3		-Os
compare		1.069	1.057	0.029	0.025	1.072
operator== 	0.461	0.990	0.980	1.022	0.201

g++ (Debian 4.3.4-6) 4.3.4. Test code attached.
#include <iostream>

const char *kStr = "Some Text. Date 09.04.2011. Time 12:32. Code A0.";

bool compare(const std::string &str)
{
	std::string test(kStr);
	bool result = true;
	for (int i = 0; i < 10000000; i++)
	{
		//result &= str.compare(test) == 0;
		result &= str == test;
	}
	return result;
}

int main()
{
	std::string str(kStr);

	if (!compare(str))
	{
		std::cout << "Failed" << std::endl;
	}

	return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]