Aaand I'm stupid.
No you are not. You are just an example that we learn everyday. :)
See, thanks to this comment, in my program I replaced
std::to_string( . . . )
with
static_cast<char>( . . . + 48) // + 48 because char(48) = '0'
And I "functionalized" my main. No big deal right?
Here are your stats before these little changes (as a reminder):
40 loops: 17,241 ms.
50 loops: 367,022 ms.
Now:
40 loops: 38 ms.
50 loops: 620 ms.
That's right: the version with a std::to_string int-to-string converter (for the purpose of an int-to-char conversion) is thousands of times slower than a static_cast<char>.
I did not know that.
From now on, I know what to use to convert my single digits ints into chars.
We always learn how our programming language(s) work(s). That's the spirit!
The new code:
#include <iostream>
#include <string>
#include <chrono>
int iterations(std::string& str, int& i, const int loops)
{
int occurences, j;
std::string newstr;
++i;
for (j = 0; j < str.length(); ++j)
{
occurences = 1;
while (str[j + 1] == str[j])
{
++occurences;
++j;
};
newstr += static_cast<char>(occurences + 48); // TURBO MODE ON
newstr += str[j];
}
if (i < loops)
return iterations(newstr, i, loops);
else
return newstr.size();
}
int main()
{
std::string str = "1321131112";
int i = 0, loops = 40;
std::chrono::time_point<std::chrono::high_resolution_clock> start = std::chrono::high_resolution_clock::now();
std::cout << iterations(str, i, loops) << " ( ";
std::chrono::time_point<std::chrono::high_resolution_clock> end = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms.)" << std::endl;
return 0;
}