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

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    Arrow 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......

    Thanks in Advance....





    Regards,
    Gunjan Sethi

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    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.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    17

    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

  4. #4
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    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.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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).

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    carpe diem
    Join Date
    Jan 2010
    Posts
    46
    - 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

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    17
    thnks a lot for your suggetion....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Regarding delay in the connection
    By byatin in forum Networking/Device Communication
    Replies: 2
    Last Post: 08-19-2008, 02:59 PM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Networking (queuing delay, avg packet loss)
    By spoon_ in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-05-2005, 11:23 AM
  4. Put A Delay To A Buffer
    By timmer in forum C Programming
    Replies: 2
    Last Post: 05-24-2005, 09:45 AM
  5. Delay
    By CanadianOutlaw in forum C++ Programming
    Replies: 4
    Last Post: 09-13-2001, 06:28 AM