Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Runtime of printf() function

printf runtime function

Best Answer oliverkatz, 02 May 2014 - 05:12 PM

If you're trying to analyze the runtime of printf, the algorithm that formats the string is not going to be the performance bottleneck. Printf is a blocking function, meaning that it waits until whatever it's printing has actually displayed before the execution of the program is resumed. The performance of printf is mostly dependant on what type of console you're displaying to. I did a test with a simple program:

#include <stdio.h>

int main()
{
  for (int i = 0; i < 1000000; i++) // 1 million iterations
    printf("hello, world\n");
  return 0;
}

And timed it in Mac OS X's terminal emulator. It got 2.597 seconds. I tried it in xterm and got 19.943 seconds. I tried running the same program on both but disabled output (by piping the result to /dev/null). In OS X's terminal emulator, I got 0.082 seconds. In xterm, I got 0.082 seconds as well.

Go to the full post


This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1 Anshdeb

Anshdeb

    CC Lurker

  • New Member
  • Pip
  • 5 posts

Posted 20 April 2014 - 04:16 PM

Hey guys,

 

I was just wondering what actually is the runtime of the printf() function. Whether it be O(n) or Ω (n), on what basis can I decide the runtime of the printf function ?

Like if it just prints a single line on the screen it can be O(1) since it would take only a particular number of CPU cycles to run that but what if printf is used in a loop where it is not predefined as to how many times the printf() function would be called, what then ? Will it be O(n) or what ?

 

Thanks 



#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts

Posted 21 April 2014 - 04:39 AM

printf() is probably going to be O(1) no matter what. With that said, code that USES printf() could be most anything. For example, the loop with printf() will be O(n), but printf() remains O(1), it's just being called n times.


Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts

Posted 21 April 2014 - 07:01 AM

A call to printf is almost always going to be O(n) in the length of its arguments - the exception being if you use the * modifier in which case it would be O(n + m) where n is the length of the arguments and m is the value given as the argument to *.

So if you call printf with constant-size arguments (like a fixed format string followed by numeric arguments), it's going to be O(1) (again with the exception of *), but if you use it to print variable strings, it's going to be O(n) in the size of those strings.

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts

Posted 23 April 2014 - 02:17 AM

A call to printf is almost always going to be O(n) in the length of its arguments - the exception being if you use the * modifier in which case it would be O(n + m) where n is the length of the arguments and m is the value given as the argument to *.

 

 Pretty sure it's more complicated than that. This is not exactly the best metric but I wrote an implementation of printf for my operating system and it's dependent on a lot of things. For example, "%.03f" and "%5c" print out the same number of characters (if the float is non-negative) but the floating-point calculation is much more involved.

 

So I say Ω(n).


Edited by dargueta, 23 April 2014 - 02:23 AM.

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 oliverkatz

oliverkatz

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 02 May 2014 - 05:12 PM   Best Answer

If you're trying to analyze the runtime of printf, the algorithm that formats the string is not going to be the performance bottleneck. Printf is a blocking function, meaning that it waits until whatever it's printing has actually displayed before the execution of the program is resumed. The performance of printf is mostly dependant on what type of console you're displaying to. I did a test with a simple program:

#include <stdio.h>

int main()
{
  for (int i = 0; i < 1000000; i++) // 1 million iterations
    printf("hello, world\n");
  return 0;
}

And timed it in Mac OS X's terminal emulator. It got 2.597 seconds. I tried it in xterm and got 19.943 seconds. I tried running the same program on both but disabled output (by piping the result to /dev/null). In OS X's terminal emulator, I got 0.082 seconds. In xterm, I got 0.082 seconds as well.



#6 sepp2k

sepp2k

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 183 posts

Posted 03 May 2014 - 05:02 AM

That's a constant factor though and does not affect the asymptotic behavior.




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download