Thread: Time functions in C/C++!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Time functions in C/C++!

    Hey!

    Im currently working on a win32 app which needs
    a timer, basically I'll set a start time when
    it starts and have end time when it stops.

    Then I'll calculate the time based on end-start.

    What is the best technique to use?

    - Mike

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    GetTickCount() returns the number of milliseconds since windows started.

    Code:
    DWORD StartTime = GetTickCount();
    
    DoSomeStuff();
    
    DWORD EndTime = GetTickCount();
    DWORD TimeElapsed = EndTime - StartTime;
    If you have more Windows questions, you can post them in the Windows board.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    12
    thanks, is there any platform independant
    method?

    - Mike

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Not that I know of...
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>thanks, is there any platform independant method?
    Similar to GetTickCount() is clock() in <ctime>, it's platform independent, but not as precise as less portable options.
    Code:
    #include <iostream>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
      long i = 0;
      clock_t start = clock();
    
      while (1)
      {
        i++;
    
        if (clock() >= start + 10)
        {
          break;
        }
      }
    
      cout<<"The counter is -- "<< i <<endl;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with time
    By Gong in forum C++ Programming
    Replies: 7
    Last Post: 01-11-2007, 02:43 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 04:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-03-2006, 12:46 AM
  4. The space time continueimnms mm... (rant)
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 32
    Last Post: 06-27-2004, 01:21 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM