Thread: Dumb I/O Performance Question

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    47

    Dumb I/O Performance Question

    I have a dumb question regarding performance using printf. Is there any difference in the code below? In the first example would there be an I/O burst everytime I called printf or would the compiler optimize the code and combine it into one burst like the second example?

    Code:
    printf("My name is Bob");
    printf("I like green apples");
    printf("My favorite color is red");
    printf("I live in a blue house");
    Code:
    printf("My name is Bob\nI like green apples\nMy favorite color is red\nI live in a blue house");

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by BENCHMARKMAN View Post
    I have a dumb question regarding performance using printf. Is there any difference in the code below? In the first example would there be an I/O burst everytime I called printf or would the compiler optimize the code and combine it into one burst like the second example?
    I would highly doubt that the compiler would perform concatenation of non-adjacent strings. And I would be highly suspect of any such compiler that "optimized" in the fashion you describe.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    So what you are saying is that version 2 is faster especially if I say had 20 different threads performaning this operation.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I would pay absolutely no attention to any perceived nanoseconds savings until I had taken the time to rigorously profile my program and find that this odd portion of code (somehow) produced an unacceptable performance hit. Until that time, I'm not going to concern myself with it. YMMV
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    printf does not insert newlines into your output.

    You could do this:
    Code:
    printf("%s",
        "My name is Bob\n"
        "I like green apples\n"
        "My favorite color is red\n"
        "I live in a blue house\n");

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    I guess the main thing I am trying to get at is that there is only one I/O burst compared to 4.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't care enough to try to work out an actual example, but I would guess you would get 4 "bursts" in both examples, since stdout is flushed on every newline character.

    Edit: obviously I'm assuming a "fixed" example where the four strings weren't printed without spaces together on one line (or until wrap).
    Last edited by tabstop; 02-22-2008 at 06:03 PM.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by BENCHMARKMAN View Post
    I guess the main thing I am trying to get at is that there is only one I/O burst compared to 4.
    I don't even know whether you can know that, or that one is supposed to care. AFAIK, a call to printf loads some buffer that the operating system gets around to handling at its leisure. Consecutive calls or no, it's the OS that is doing your burst, not the code. Line buffering, etc. options requested and what-not, it's still the OS's call.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    It depends.

    Usually, stdout is buffered, unless you changed that.

    So it should be actually written to disk in a few or one burst, but it might not if it filled up the buffer.

    Using more than one function call will usually probably make the program run on the CPU a bit slower, but it probably won't make enough difference to slow down the file io operation in general, because the operation is file io bound.

    If you do this:

    "String" "string2" then the strings will be made one by the preprocessor like this:

    "Stringstring2"

    And you theoretically have lower overhead.

    But it probably won't make that much difference unless that's all this program does and on something other than files (like ramdisks or something).
    Last edited by robwhit; 02-22-2008 at 06:17 PM.

  10. #10
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%."

    I believe most people will agree with this. Indeed, i do, since when i start thinking first about optimizing my code, it often gives bad-looking merely-working code.

  11. #11
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    The best performance hit can be gained by using, on this simple case of unformatted output, io functions that do not care about formatting.
    Code:
    char s[] ="A really really long string spanning over many many lines\
    ,\nand having embedded\t\tcharacters\n\
    and various other things and needed to be\n written very fastly\
    to the standard output.\nHow to do it?\n";
    
    fwrite(s, sizeof(s), 1, stdout);
    That's probably as fast as it can get...
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Efficencly is actually a secondary effect of what I am trying to accomplish. I'm creating 20 thread that print debut and using multiple printf statments causes the output to be interleaved.

  13. #13
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    So you were wondering what would happen if using multiple printf statements?
    Well i think that chances are that a thread might be stopped before finishing all of it's printf statements, and another thread starts printing it's own and then again it might even stop during the execution of a printf statement, unless you somehow do a lock on the output sections of your threads the output will most certainly be interleaved.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by xuftugulus View Post
    So you were wondering what would happen if using multiple printf statements?
    Well i think that chances are that a thread might be stopped before finishing all of it's printf statements, and another thread starts printing it's own and then again it might even stop during the execution of a printf statement, unless you somehow do a lock on the output sections of your threads the output will most certainly be interleaved.
    When using VS with MT version or CRT library printf will definitely NOT stopped inside outputting the string

    However, strings posted from different printfs (or over output functions) could and will interleave.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    47
    Quote Originally Posted by vart View Post
    When using VS with MT version or CRT library printf will definitely NOT stopped inside outputting the string

    However, strings posted from different printfs (or over output functions) could and will interleave.
    What is VS with MT version or CRT library printf ? Is that the standard printf()?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. very dumb question.
    By Blips in forum C++ Programming
    Replies: 14
    Last Post: 11-08-2005, 09:37 AM
  2. Dumb question
    By dragon2309 in forum C Programming
    Replies: 18
    Last Post: 10-29-2005, 03:27 PM
  3. C++ File I/O question
    By zero_cool in forum C++ Programming
    Replies: 3
    Last Post: 08-16-2005, 10:43 AM
  4. File I/O Question
    By Sentral in forum C++ Programming
    Replies: 4
    Last Post: 07-06-2005, 04:49 AM
  5. dumb question...
    By Sebastiani in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-28-2002, 10:35 AM