Thread: fprintf in subroutine and runtime

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    6

    fprintf in subroutine and runtime

    This may be a well-known phenomenon, but I've only just noticed it and am not sure why it happens.

    I've got a basic little subroutine called from main that runs two nested for loops and takes 13 realtime seconds to run. If I include an fprintf at the end of the routine, after complettion of the for loops, it takes nearly 4 minutes. If I include an fprintf at the end of main, runtime is again 13s.

    Can anyone explain to me why this is?

  2. #2
    User
    Join Date
    Jan 2006
    Location
    Canada
    Posts
    499
    Maybe it's got some issues dumping the stack after running fprintf.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    I/O is pretty slow compared with other things done by the computer. For example, times to seek, read, and write data to disk are typically measured in milliseconds whereas times for completion of machine instructions and accessing data in RAM are measured in microseconds (or even smaller units).

  4. #4
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    >> Maybe it's got some issues dumping the stack

    What is 'dumping the stack'?

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    http://cboard.cprogramming.com/showt...tf+performance
    My guess is, adding the printf() is actually printing a variable which was otherwise unused, and the compiler had optimised it out.

    By printing it (and hence using it), it forced the compiler to include a whole bunch of expensive code which was otherwise optimised out.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    6
    Quote Originally Posted by Salem
    My guess is, adding the printf() is actually printing a variable which was otherwise unused, and the compiler had optimised it out..
    That's what it was. Thanks. I should have realized it.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Salem
    http://cboard.cprogramming.com/showt...tf+performance
    My guess is, adding the printf() is actually printing a variable which was otherwise unused, and the compiler had optimised it out.

    By printing it (and hence using it), it forced the compiler to include a whole bunch of expensive code which was otherwise optimised out.
    While the phenomenon you describe (adding I/O operations changing how effectively a compiler does optimisation) can have some effect, those effects are rarely of the order of those described in the original post.

    The more likely reason is that I/O is slower than many other operations on a machine, and doing many I/O operations consumes both CPU time and actual (clock) time.

    It is a pretty fair bet that
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i, j;
        for (int i = 0; i < 10000; ++i)
       {
            for (int j = 0; j < 10000; ++j)
            {
                 some_complex_calculations();
            }
       }
    
       print_results_of_calculations();
    }
    will run noticeably more quickly on most systems than;
    Code:
    #include <stdio.h>
    
    int main()
    {
        int i, j;
        for (int i = 0; i < 10000; ++i)
       {
            for (int j = 0; j < 10000; ++j)
            {
                 some_complex_calculations();
    
                 fprintf(stdout, "%s\n", "Hello");   /* only addition */
            }
       }
    
       print_results_of_calculations();
    }

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > While the phenomenon you describe
    Read the OP - the number of printfs didn't change - they simply moved the single printf from the end of the function to main to get the huge difference.

    For sure putting printf inside a nested loop will make a difference.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed