speed of printf()

  • Hi all,
    I have a tiny program. it has a simple loop with a printf().



    TickCounter = GetTickCount();
    for (i = 0; i < 256; i++)
    {
    printf("Write Error");
    }
    printf("Loop Time: %d ms\n", GetTickCount() - TickCounter);


    my proram is only program to run. the last printf() shows me :
    Loop Time: 1530 ms
    and anothe thing if I add "\n" to the first loop I get this:
    Loop Time: 4230 ms


    Anyone can tell me what happens?
    Thanks

  • Hello,
    The printf() inside your loop is responsible for the slow execution. Remove printf() and it will be very fast.
    Regards
    Holger

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • When using '\n' the screen must be scrolled a lot if the text ist output to an console window. This uses a lot processor resources. Without '\n' the screen must be scrolled not so often.

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.

  • Quote

    But my question is why the printf takes a lot of time?


    Because it must render the text, i.e. draw the pixels of the characters that constitute the text. And if there is scrolling involved, the whole display content must be moved. We have a graphical display, meaning a VGA display in HiColor (=16 bits/pixel) takes about 600K of data. And the standard console is inside a window, which means clipping and special handling of the borders is required. All of this has to be done by the CPU.


    You can not compare this to a PC output on a text display, where the CPU has to do nothing for the text output itself.


    Regards,


    H. Keller

    F&S Elektronik Systeme GmbH
    As this is an international forum, please try to post in English.
    Da dies ein internationales Forum ist, bitten wir darum, Beiträge möglichst in Englisch zu verfassen.