0

Im testing this code:

string teststring = ""; int i = 0; while ( i < 100000000 ) { i++; } 

The execution time is: 0.359 s.

Then I try to do the exact same test again, but this time Ive added one extra line within the loop:

string teststring = ""; int i = 0; while ( i < 100000000 ) { i++; teststring += "something random"; } 

The execution time is 4 s.

Its one extra line, should it really take that much longer? Can I do something different to improve this?

9
  • 2
    int i = 0; while ( i < 100000000 ) {i++;} likely gets optimized to int i = 100000000; or something similar. Commented Aug 1, 2018 at 17:10
  • 5
    Beware that appending text to a string is a significantly more involved process than incrementing an integer. Commented Aug 1, 2018 at 17:11
  • 2
    You can play around with goldbolt.org to see what exactly the compiler produces. Don't forget optimization flags, as profiling unoptimized code is almost never relevant. Commented Aug 1, 2018 at 17:12
  • 1
    It likely gets optimised away altogether. I have a short blog article on this at latedev.wordpress.com/2011/10/15/the-joy-of-benchmarks Commented Aug 1, 2018 at 17:12
  • 2
    "Its one extra line, should it really take that much longer?" I can easily write one line of code that will make your program slower in infinite times. What is the point? Commented Aug 1, 2018 at 17:24

2 Answers 2

4

How would teststring += "something random"; be implemented? Something like:

str = "something random"; for( int i = 0; i < sizeof(str); i++ ) teststring[teststring.length+i] = str[i]; 

Not to mention if teststring isn't big enough, it has to make a new region of memory twice as large, and copy every single byte over, and then continue (teststring will grow very large in your code, making that copy operation quite expensive). This is very complicated in comparison to i++, so it makes sense that the latter is much faster.

Sign up to request clarification or add additional context in comments.

4 Comments

So the string size should be predefined and as precise as possible, if I have understood you correctly
@QrazyNeo I'm not sure what you mean. predefined and as precise as possible?
Oh, if you mean to avoid teststring having to make a new region of memory, yes absolutely. Better to make it a bit bigger too since the copy operation is expensive and a bigger string is no big deal. string::reserve will help. Even if you do this though, the inner for loop iterates 16 times, so it should theoretically still be much longer than the i++ alone, even if the CPU tries to run as much as it can in parallel (Doing all the teststring[end+i] writes at the same time since they're independent).
Thank you very much. This helped a little. Will also take a closer look at optimization as others here have suggested
1

In addition to Nicholas Pipitone's point, an optimizing compiler can legitimately eliminate the original for loop and replace it with:

int i=100000001;

The optimizing compiler could do the same for the string concatentation, but it probably didn't bother catching that case (possibly because the string concatentation function may indicate it can throw exceptions, which the compiler may not know how to deal with).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.