I was surprised when I benchmarked this function:
int f(int N = 999) {
int nMax = 0;
for (int i = 1; i <= N; ++i)
for (int j = i; j <= N; ++j) {
string digits = to_string(i*j);
string rDigits = digits;
reverse(rDigits.begin(), rDigits.end());
if (digits == rDigits)
nMax = max(i*j, nMax);
}
return nMax;
}
with VS2012, VS2013 (release, /O2) and MinGW 4.8.0, 4.8.1 (-Ofast) on Windows 7 32-bit and 64-bit. I noticed that MinGW built versions run about 13 times slower than VS ones. Is this a problem with implementation of to_string()
and reverse()
? Any other reasons?
The code I used is here: https://github.com/pauljurczak/Benchmark-2/blob/master/benchmark.cpp
EDIT
I narrowed down the problem to std::to_string()
function, which is about 16 times slower with MinGW then with VS. I used this snippet for testing:
int f(int N = 100000) {
int len = 0;
for (int i = 0; i <= N; ++i)
len += to_string(i).length();
return len;
}
When I compile it with g++ 4.7.3 on Ubuntu, performance is about the same as VS2012.