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
Solved

Elapsed time

Posted on 1998-12-12
9
642 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

837 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