Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Elapsed time

Posted on 1998-12-12
9
Medium Priority
?
648 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

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

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

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 200 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

636 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