Thread: output buffers vs. internal ones?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    88

    output buffers vs. internal ones?

    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.

  2. #2
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    i am pretty sure that fragment one is the fastest.

    in fragment 2, i am pretty sure that append is slower than just output with cout<<

    i am sure that fragment 3 is the slowest becoues not only are you doing the same thing as fragment one, but you are also copying a viariable into a function, which takes alot of time in a case where you would be doing this mulitiple times

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  2. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM