I've been thinking about optimisation recently wrt a web app I'm working on. I get the impression that outputting (calling cout) is generally a bottleneck on these kinds of program but I'm not too sure about that yet.
What I wanted to know is what the difference in the following is, in terms of memory usage, if anyone knows. Can the compiler optimise these so 1 and 2 are both equivalent?
Code:
string s1( const string& str ) { return str; } // just imagine s1 does something to str
//fragment 1
cout<< s1("h") << s1("e") << s1("l") << s1("l") << s1("o") << s1(" ")
<< s1("w") << s1("o") << s1("r") << s1("l") << s1("d");
//fragment 2
cout<< s1("h") + s1("e") + s1("l") + s1("l") + s1("o") + s1(" ")
+ s1("w") + s1("o") + s1("r") + s1("l") + s1("d");
//fragment 3
void s2( const string& str ) { cout<< str; }
s2("h");
s2("e");
s2("l");
s2("l");
s2("o");
s2(" ");
s2("w");
s2("o");
s2("r");
s2("l");
s2("d");
I know how stupid they look but they illustrate my situation.
Can anyone tell me how different these three actually are?
I guess that frag 3 would be the slowest being that it calls cout repeatedly and that frag 2 uses more memory than 1 since it creates a temporary (internal) string before passing the whole thing to the output buffer but I can't even be sure if that's how they work. I am struggling to figure it out.