C Programming Timing

This is a discussion on C Programming Timing within the C Programming forums, part of the General Programming Boards category; I am a begineer to C Programming. Is there a command to give processor independent accurate timing. Let me explain. ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Unhappy C Programming Timing

    I am a begineer to C Programming.

    Is there a command to give processor independent accurate timing.

    Let me explain.

    I want to write the following program:

    1) Print x to the screen
    2) Wait for 5secs
    3) Print x to the screen
    4) Jump to 1)


    I want the program to wait 5secs between printing "x" no matter what PC processor I use.

    Hence, I can not have:

    1 Printf("x");
    2 If (x=0;x<200000;x++);
    3 Go to 1

    I must have something like:
    1 Printf("x");
    2) Get time now;
    3) Let timstart=Get time now
    4) Let timstart=Get time now + 5seconds
    4) Get Time
    5) IF GET Time < timstart go to 4 else go to 1


    Thanks for you help

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    There are individual API that can sleep for "around" n milliseconds.
    However, most Operating Systems are not real time and can therefore not wait for exactly the amount you specify.
    You might stat by telling the OS, I think, or give a search for sleep a go.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    C has a time and date library that lets you do stuff like that.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void waitFor( double seconds )
    {
        time_t start = time( NULL );
        double diff = 0;
    
        do
        {
            diff = difftime( time( NULL ), start );
        }
        while ( diff < seconds );
    }
    
    int main( void )
    {
        puts( "x" );
        waitFor( 5.0 );
        puts( "x" );
    
        return 0;
    }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,022
    No!
    This will consume 100&#37; CPU while waiting!
    Use appropriate Sleep functions. They use no CPU while waiting.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    sleep() on *nix systems. Sleep() on Windows systems. Note the case of the first letter. Also, beware what arguments to give it since they accept different arguments.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Some operating systems can do this, at the expense of everything else in the system. And not just the humans using it, but all the devices wanting to interrupt the system too.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Thank You

    Thank you for all you replies especially.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Timing basic operations in C++
    By StevenGarcia in forum C++ Programming
    Replies: 9
    Last Post: 09-18-2007, 03:10 AM
  2. Performance Timing Function
    By rosicky2005 in forum C++ Programming
    Replies: 11
    Last Post: 05-31-2007, 04:09 PM
  3. My Timing System
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 01-01-2006, 11:43 PM
  4. Games - timing
    By Magos in forum Game Programming
    Replies: 7
    Last Post: 03-06-2004, 11:32 AM
  5. Timing in Windows
    By steinberg in forum Windows Programming
    Replies: 3
    Last Post: 07-14-2002, 01:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21