Ian Lance Taylor <iant@xxxxxxxxxx> writes: > Дмитрий Оксенчук <oksenchuk89@xxxxxxxxx> 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. Second row of results table shows that -O0 was faster than -O2 and -Os was faster than -O0. I'm not using -march and -mtune options, version of g++ is 4.3.4 from Debian stable, CPU Intel Core2 Duo T8100.