Elapsed time

Posted on 1998-12-12
Last Modified: 2013-11-20
I have the following situation:
CTime startTime = CTime::GetCurrentTime();// ... perform time-consuming task ...
CTime endTime = CTime::GetCurrentTime();
CTimeSpan elapsedTime = endTime - startTime;

The problem is that the time calculated, is the actual time, I want the time spent only on the task. for example if I move from the program and launch something else. I dont want to include that time. I know how to it in Unix but how can I do it here under NT
Question by:maj030598
  • 4
  • 2
  • 2
  • +1

Expert Comment

ID: 1326106
Create a third variable called something like time_to_ignore.  Then, whenever the window loses input focus, start counting seconds.  When the window regains the focus, stop counting.  Subtract this time from elapsedTime.  Does that make sense?

Author Comment

ID: 1326107
I cant do that. Because the program is running in the background and from task manager I can see it is using about 99% of the CPU time. when I launch something else it drops to around 65% and then back to 99%. In other words the program is running, and it runs for a long time like 30hours. I want to be able to use the PC. while it is running in the background.

Expert Comment

ID: 1326108
Say you get a time.

make variables
hour[3], min[3], sec[3], ss[3];

read the time and assign each part. (if the date is involved then add date variables.

then you might get

time elapsed.
hour[2]=hour[0]-hour[1]; //which is 1
min[2]=min[0]-min[1]; //0
sec[2]=sec[0]-sec[1]; //0

so 2:00:00:10 elapsed.  and so forth.  Take it as my answer, I'll leave it as a comment, to leave it open for suggestions. :)
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.


Expert Comment

ID: 1326109
OOOOPS  sorry in the time elapsed

hour[1]-hour[0] etc.. sorry!!!!! :)

Author Comment

ID: 1326110
This will calculate the actual time, not the time that the system is spending on my function. I want only the time NT spends on my application. to clear my point here is the unix code:
/* measure exact time, avoid the error of rounding from time() function;
you just have to call it before and after the routine you need to measure
the time for and take the difference*/
float TimerRoutine(void)
 struct tms TimeBuffer;
 clock_t ClockTicks;
 float Seconds;

 ClockTicks = sysconf(_SC_CLK_TCK);
 /* # of clktcks/sec for
 this system */

 Seconds = (float) (TimeBuffer.tms_utime +
 Seconds /= ClockTicks;

LVL 86

Accepted Solution

jkr earned 50 total points
ID: 1326111
Simply use the Win32 API 'GetProcessTimes()' to calculate the time your process consumed, e.g. (psudocode ;-)

DWORD   GetProcessTicksTotal    (   HANDLE    hProcess)
    DWORD       dwRC;
    FILETIME    ftCreate,   ftExit,     ftKernel,   ftUser;    

    if  (   GetProcessTimes (   hProcess,
            // Horrible, disgusting hack!  The two lines below basically grab the
            // contents of a FILETIME structure and store it in a 64 bit integer.
            LONGLONG    tUser64     = *( LONGLONG *)    &ftUser;
            LONGLONG    tKernel64   = *( LONGLONG *)    &ftKernel;
            DWORD       tUser, tKernel;

            // The LONGLONGs contain the time in 100 nanosecond intervals (now
            // there's a useful unit of measurement...).  Divide each of them by
            // 10000 to convert into milliseconds, and store the results in a
            // DWORD.  This means that the max time before overflowing is around
            // 4 Million seconds (about 49 days)
            tUser   =   ( DWORD)( tUser64 / 10000);
            tKernel =   ( DWORD)( tKernel64 / 10000);
     else   PANIC!!!!

    dwRC    =   tUser   +   tKernel;

    return  (   dwRC);

Call this when your program starts and when it ends - the difference between the return values will be the time only your process consumed (in milliseconds)

Author Comment

ID: 1326112
to jkr:
I have exactly the following:
1: DWORD m_EllapsedTime;
2: CTime t1 = CTime::GetCurrentTime();
3: BuildSprintTree (m_Traverse, m_SampleList);
4: CTime t2 = CTime::GetCurrentTime();
5: m_EllapsedTime = t2.GetTime() - t1.GetTime();
at line 2, I should replace it with
DWORD t1 = GetProcessTicksTotal (what should I put here)
also for line 4?
LVL 86

Expert Comment

ID: 1326113
Just replace lines 2 and 4 with it, e.g.

 DWORD m_EllapsedTime, t1, t2;
 t1 = GetProcessTicksTotal (GetCurrentProcess());
 BuildSprintTree (m_Traverse, m_SampleList);
 t2 = GetProcessTicksTotal (GetCurrentProcess());
 m_EllapsedTime = t2 - t1; // in milliseconds!!!


Author Comment

ID: 1326114
Thanx a lot

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 43
bobThere java chalenge 45 92
Complete beginner needs help making a cron job 9 118
Change owner from userA to server Local Administrators 7 64
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question