Monday 15 August 2011

c - printf slows down my program -


I have a small program to calculate the hash (for hash tables). This code looks very clear, I hope, but there is something unrelated in it that makes me angry.

I can easily generate about one million hash in about 0.20.3 seconds (approx / USR / bin / time benchmark). However, when I put printf () in the loop, the program is slowed down to about 5 seconds.

  1. Why is this?
  2. How to make it faster and faster? What can be Mmapp ()?
  3. How about stdlibc designed, and how can it be improved?
  4. How can the kernel improve it? How to make the throughput faster on local "files" (sockets, pipes, etc.)?

I look forward to interesting and detailed answers thanks.

PS: This is for a compiler build toolset, so do not shy to get information. Although there is nothing to do with that problem, I just want to show that the details give me the interest.

Appendix

I'm looking for more Programmatic approach to solution and explanation is actually piping, but my "user" What does not have control over.

Of course, I'm still testing, which will not be done by "normal." But this fact does not change that a simple printf () slows down a process, which is the problem , I am trying to find an optimal programmatic solution for this.


Appendix - Stunning results

Reference time is plain within a TTY The printf () is for calls and takes about 4 minutes to 20 seconds.

/ Dev / pts console) increases the speed of the output for approximately 5 seconds.

This is approximately equal to approximately 8192 when using setbuffer () in my test code size of 16384: approximately 6 seconds .

SetBuffer () Apparently does not have any effect while using it: it can be used at the same time (about one TTI about 4 minutes, about one PTS 5 Seconds). / P>

Amazing , if I'm starting a test on TTY1 And then switch to another TTI , this is only one PTS: approximately 5 seconds.

Conclusion : The kernel does something that can be accessed Has to be done with and user friendliness!

Generally, if you are active on TTI, or you switch to any other TTY, then it should be equally slow.


Lesson : When output-intensive programs run, switch to another TTE!

Unbuired output is very slow

By default, stdout is completely buffer, even if connected to a terminal, stdout is given either unbreakable or line-buffer. Try switching to buffering to use setvbuf () for

stdout , like this:

  buffer [8192]; Setwuf (standout, buffer, _IOFBF, sif (buffer));  

No comments:

Post a Comment