2

Is there a good way to slow down program output? usleep and nanosleep don't slow it down enough, delay doesn't work, and sleep keeps freezing my program. I am using Linux since I think this makes a difference in what I have to use.

These were the values I tried. I would like at least a 5 second delay.

//usleep(1000000);
sleep(5);
//delay(3000);
//nanosleep(999999999);
//usleep(102400);
cokedude
  • 379
  • 1
  • 11
  • 21
  • Why the hell you may want to do that? – Andrejs Cainikovs Apr 01 '14 at 10:26
  • 2
    If you want to see the output much slower, you can step it through a debugger. – Bartlomiej Lewandowski Apr 01 '14 at 10:27
  • 4
    Do you really want to slow it down, or just stop it from scrolling off the screen? If the latter, why not pipe it through `more` or `less`? – pat Apr 01 '14 at 10:27
  • Have you tried a timer or a stopwatch? Look inside time.h. – ouflak Apr 01 '14 at 10:27
  • 1
    Use larger values to usleep(). – mah Apr 01 '14 at 10:28
  • 1
    If you really slow down stdout and your program fills the buffer and cannot write to it anymore, it will also freeze your program. – Thilo Apr 01 '14 at 10:29
  • You may find some useful suggestions here: http://stackoverflow.com/questions/52187/virtual-serial-port-for-linux – Ruud Helderman Apr 01 '14 at 10:35
  • using nanosleep you should be able to slow down for even seconds. how much more delay do you need? – Bas Apr 01 '14 at 11:36
  • I am doing a modified version of game of life. My program is going to fast to see if it is doing what I want. – cokedude Apr 01 '14 at 13:10
  • 1
    If you want to have extremely slow terminal output, run your program on Windows' shell. But in reality, `usleep(1000000)` will give you 1s of delay which should be slow enough to see how the changes are propagated in the game of life (actually it would be really slow). `usleep(5000000)` is 5s as you've been asking for! – Shahbaz Apr 01 '14 at 13:19
  • For checking purposes, you could send the output to a log file and read over it after the program has executed. – Jack Apr 01 '14 at 13:21
  • I actually have an article on filter programs that may suit your purposes - it's a filter that slows down output to a specific number of characters per second - see http://powerfield-software.com/?p=172 – paxdiablo Sep 01 '14 at 08:08

1 Answers1

2

There is no core function that I am aware of that will allow a system call for sleep. You could use a for loop e.g.

void sleep()
{
   int i = 0;
   while (i < 1e6)  { i++; }
}

Its rather inefficient but for the purposes of slowing down your output it will suffice. Also I haven't tested this code, so it might require some tweeking. It also depends how fast your PC is, but at least its portable.

If you can move to C++ then, from Sleep for milliseconds, you can use usleep which accepts an argument in microseconds:

#include <unistd.h>

unsigned int seconds;
...
usleep(seconds*1000*1000);
Community
  • 1
  • 1
Aaron
  • 7,015
  • 2
  • 13
  • 22