# help.....!!!!!how much delay i will get by using delay(1)

• 03-22-2010
gunjansethi
help.....!!!!!how much delay i will get by using delay(1)
hello all,

i m getting problem to generate a delay by using a simple delay function
my doubt is--how much delay did i receive whn i call delay(1).

code like as--
Code:

``` delay(int a) { int i;     for(i=0;i<a;i++)     {} }```
how much delay did i receive from that.....

and if i need to get delay of 10msec what shoud i do......

Regards,
Gunjan Sethi
• 03-22-2010
cas
It depends. If your compiler is smart, it will optimize that loop into nothing. You can avoid this by using “volatile int”, but that's not really useful.

How long the loop takes depends on how fast your computer is, probably (I assume all compilers will choose roughly equivalent code for the loop). So to find out what it takes for 10ms, time the loop and see how long it takes with various values. You'll probably be able to quickly ascertain roughly how many loops equals whatever value you want.

But don't do that. This is an absolutely terrible way to introduce delay. It's dependent on the system speed, and so will perform differently on different computers. Ever play an old DOS game on a modern computer (perhaps through Dosbox), and have it go at roughly the speed of light? This is because the original programmers assumed that the (slow) speed of contemporaneous computers would provide necessary delays. That's the same principle you're trying to use.

Instead, find out what your operating system provides for sleeping. POSIX systems, for example, have nanosleep(), and that might suit you. Other OSes probably have their own ways of doing delays, and these are much better than your proposed method. They will be consistent (more or less) across machines, and they should not burn CPU the way a busy loop would.
• 03-22-2010
gunjansethi
re:
thnks a lot....

My OS is windows XP..
and using Turbo C..

pls let me know if is there any other option to generate delay because my end aim is to transmit 1000 packets of 8byte at a baud rate of 1152k with the delay of 10msec betwween two consecutive transmission on serial port...

Regards,
gunjan sethi
• 03-22-2010
UltraKing227
try the sleep; command. by the way, i suggest you atleast get Code::Blocks
or Dev-cpp (or VisualStudio, if you can afford it), Turbo C is an almost weak
compiler and IDE compared to today.
• 03-22-2010
In Turbo C, it's delay(milliseconds). And in sleep(seconds). You should be including dos.h, but TC may include that for you, automatically.

Windows will give you AT LEAST the delay you request, and frequently a bit more. If I really needed just 10 ms, I would use delay(7 or 8), and see if that wasn't close to 10 ms., on a system that wasn't busy at that time.

A slick idea someone posted here previously, was to use a loop, and see what the difference was between what your delay was, and what you requested. That amount was then added or subtracted to the amount you were requesting for the next iteration of the loop.

That's a very useful idea if you have repeated delays that you need to correct for.

For Microsoft's Visual C compilers, it's Delay(in seconds), and Sleep(in milliseconds).
• 03-22-2010
Salem
> My OS is windows XP..
> and using Turbo C..
Have you considered the age difference between these two things?

How critical is the delay?
On all desktop operating systems, your user-land code can be delayed for all sorts of reasons by the OS (and for a lot longer than 10mS). All delays should be regarded as MINIMUM requests, not absolute guarantees.

A native win32 application (not the 16-bit emulation you're running at the moment), run at the "real-time" priority might just be able to maintain 10mS gaps for 10 seconds (so long as absolutely nothing else is happening).
• 03-22-2010
doia
- first sugggestion: increase priority of your app to real time and use Sleep(10) in between outputs

If that doesn't work you can...
- look into getting a real time OS
- you said you want to output your stream through a port (serial port or USB). There is hardware out there you can use that controls baud rate for you even if your pc's scheduling of tasks is not consistent look into FTDI's FT4232H Mini Module
• 03-23-2010
gunjansethi
thnks a lot for your suggetion....