Strange optimization results

Ian Lance Taylor iant@google.com
Sun Apr 10 19:21:00 GMT 2011


Дмитрий Оксенчук <oksenchuk89@gmail.com> writes:

> 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.

It's pretty hard to tell whether this is a bug or a feature in and of
itself.  -Os optimizes for code size.  -O2 optimizes for runtime
performance.  I don't see any particular reason to expect
std::string::compare or operator== to be faster or slower with either
one.

If you showed that -Os was faster than -O2, or that -O2 was smaller than
-Os, then I would say that you have found a bug.  However, I would
qualify that by asking whether you are using the -march and -mtune
options appropriate for the specific CPU on which you are doing the
testing.  And I would qualify it further by saying that micro-benchmarks
are notoriously unreliable when it comes to predicting performace in
real code.  They are not always wrong, but they often are.

Ian



More information about the Gcc-help mailing list