Low Processor Usage Timing Function

This is a discussion on Low Processor Usage Timing Function within the C++ Programming forums, part of the General Programming Boards category; I'm trying to write a function that puts the amount of time in seconds the program has been running into ...

  1. #1
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399

    Low Processor Usage Timing Function

    I'm trying to write a function that puts the amount of time in seconds the program has been running into a global variable. I've got one, but it eats CPU time like crazy.

    int main()
    {
       curTime = 0;
       DWORD nThreadID;
       CreateThread(0, 0, TimeHandler, NULL, 0, &nThreadID);
       return 0;
    }

    DWORD WINAPI TimeHandler(void* a)
    {
       time_t firstTime;
       firstTime = time(NULL);
       for(;;)
       {
          curTime = time(NULL) - firstTime;
       }
    }

    curTime is a global variable.

    There must be a better way to do this. Any suggestions on how to decrease the amount of CPU time this takes, or maybe a totally different approach all together?

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    You could always write a function, or, if variable behavior is desired:

    Code:
    class curTimeclass
    {
       public:
       operator time_t()
       {
         return time()-starttime;
       }
       curTimeclass(): starttime(time(0)) {}
      private:
       time_t starttime;
    };
    
    curTimeclass curTime;
    
    int main()
    {
      ...
      cout << "Program run-time:" << curTime << endl;
      ...
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Thanks for your responses. Let me re-word my problem and try again. I've programmed a good bit in both C++ and VB. Basically what I want to do is a whole lot like an event in VB. I want to run a function every two seconds. However, if I try to do it by setting up an infinite loop that checks to see if the time has passed, it eats CPU time like crazy. What's a better way to do this?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Timing and Ending a Function
    By mikeman118 in forum Windows Programming
    Replies: 5
    Last Post: 12-20-2007, 11:16 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

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