Solved

Elapsed time

Posted on 1998-12-12
9
638 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
OOOOPS  sorry in the time elapsed

hour[1]-hour[0] etc.. sorry!!!!! :)
     ^-------^
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:maj030598
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanx a lot
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

15 Experts available now in Live!

Get 1:1 Help Now