Work out how many seconds a thread ran for

This is a discussion on Work out how many seconds a thread ran for within the Windows Programming forums, part of the Platform Specific Boards category; Code: #include "stdafx.h" #include <Windows.h> DWORD WINAPI test_thread(LPVOID Param) { MessageBox(NULL,"test_thread started ","",0); for(int i = 0; i < 99999999999; ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    310

    Work out how many seconds a thread ran for

    Code:
    #include "stdafx.h"
    #include <Windows.h>
    
    DWORD WINAPI test_thread(LPVOID Param)
    {
        MessageBox(NULL,"test_thread started ","",0);
    
        for(int i = 0; i < 99999999999; i++)
        {
    
        }
        //i would like to work out how many seconds that loop ran for
    
        MessageBox(NULL,"test_thread over","",0);
    
        return 0;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        HANDLE test_thread_handle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)test_thread,NULL,NULL,NULL);
        WaitForSingleObject(test_thread_handle,INFINITE);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    You can save the time befor the loop and check it after:

    Code:
    //#include <time.h>
    
    //involved variables
    clock_t start_time, end_time;
    double elapsed_time;
    
    //check start time
    start_time = clock();
    
    //do the loop
    for(...) {
        ...
    }
    
    //check end time
    end_time = clock();
    
    //and get the seconds
    duration = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
    Hope that helps
    Niara

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Or even easier: use the win32 GetTickCount instead time() functions to get the milliseconds before and after, then just deduct. On the MSDN/GetTickCount() theres some better explanation than mine, also covers high resolution timers, timer objects, etc

    Niara

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Thread-specific data not work fine for me in pthread
    By ppdouble in forum C Programming
    Replies: 5
    Last Post: 01-03-2013, 09:45 AM
  2. Replies: 3
    Last Post: 11-19-2011, 11:01 PM
  3. Replies: 2
    Last Post: 10-25-2011, 06:23 PM
  4. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  5. URLDownloadToFile dont work in worker thread
    By hanhao in forum Windows Programming
    Replies: 2
    Last Post: 07-02-2007, 12:49 AM

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