Solved

Elapsed time

Posted on 1998-12-12
9
639 Views
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
0
Comment
Question by:maj030598
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 6

Expert Comment

by:thresher_shark
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?
0
 

Author Comment

by:maj030598
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.
0
 
LVL 1

Expert Comment

by:The_Brain
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
hour[0]=3
min[0]=4
sec[0]=2
ss[0]=30
//end
hour[1]=4
min[1]=4
sec[1]=2
ss[1]=40

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
ss[2]=ss[0]-ss[1];//10;

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. :)
0
 
LVL 1

Expert Comment

by:The_Brain
ID: 1326109
OOOOPS  sorry in the time elapsed

hour[1]-hour[0] etc.. sorry!!!!! :)
     ^-------^
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:maj030598
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 */
 times(&TimeBuffer);

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


return(Seconds);
}
0
 
LVL 86

Accepted Solution

by:
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,
                                &ftCreate,
                                &ftExit,
                                &ftKernel,
                                &ftUser
                            )
        )
        {
            // 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)
0
 

Author Comment

by:maj030598
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?
0
 
LVL 86

Expert Comment

by:jkr
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!!!


0
 

Author Comment

by:maj030598
ID: 1326114
Thanx a lot
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
difference between String.subString() and String.subSequence() 6 177
lucky13 challenge 11 124
matchUp  challenge 9 94
firstChar challenge 13 110
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now