Time functions in C/C++!

This is a discussion on Time functions in C/C++! within the C++ Programming forums, part of the General Programming Boards category; Hey! Im currently working on a win32 app which needs a timer, basically I'll set a start time when it ...

  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, 01:43 PM
  2. need help in time zone
    By Gong in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2007, 03:44 AM
  3. Help with assignment!
    By RVDFan85 in forum C++ Programming
    Replies: 12
    Last Post: 12-02-2006, 11:46 PM
  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

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