相关文章推荐
俊逸的石榴  ·  toString()、String.valu ...·  2 年前    · 
神勇威武的猴子  ·  scikit learn - The ...·  2 年前    · 
安静的自行车  ·  如果使用 ...·  3 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm calling GetProcessTimes() periodically in a loop which does the very same thing on each iteration, but it seems to generate the same results every time and only changes time to time. Is it a normal behaviour ? Shouldn't the results change a little bit over time ?

void ImCalledPeriodically() {
    static const DWORD  dwPid = ::GetCurrentProcessId();
    static const HANDLE hProc = ::OpenProcess( PROCESS_QUERY_INFORMATION, FALSE, dwPid );
    static FILETIME ftUnused1, ftUnused2; // Unused, mandatory parameters.
    ULARGE_INTEGER uliUsr, uliKrn;
    ::GetProcessTimes( hProc, & ftUnused1, & ftUnused2, (_FILETIME *)& uliKrn, (_FILETIME *)& uliUsr);
    printf("usr=%I64d krn=%I64d", uliUsr.QuadPart, uliKrn.QuadPart );
    // etc...

The output values only change time to times, for example :

641002, 641002, 641002, 641002 1092007, 1092007, 1092007, 1092007 etc...

Shouldn't it change a few every time ? Is there some kind of refresh rate internal to the function ?

Thanks for your help.

Uh, why are you passing a pointer to a ULARGE_INTEGER to a function that expects a pointer to a FILETIME structure? – Cody Gray Aug 7, 2013 at 15:52 That's a good question. It's not my own code and it manipulate things i've nevet got close to x) – Virus721 Aug 7, 2013 at 15:56 That doesn't make it correct. Pass the function the type it wants, then convert it to the type you desire. The two types are not the same. – Cody Gray Aug 7, 2013 at 16:01

You are probably noticing that the values are only changing about every 16 ms, which is the default time interval that Windows uses for its timers and the thread time quantum. So, if you are making the calls in a relatively tight loop, you will get repeating values.

On top of this is the fact that the times show actual CPU consumption. If your process does little or no work in between calls, the times will not increase.

Thanks but actually there is more than a second between each call so i guess this is not what's causing the problem. Shouldn't the times increase even by a tiny bit, even if the process does not much ? – Virus721 Aug 7, 2013 at 15:57 @Virus, they cannot increase by a tiny bit, because the minimum increase interval is 16 ms which I described in my answer. So, until you consume 16 ms of CPU, there will be no increase. – Michael Goldshteyn Aug 7, 2013 at 16:04 @Virus721: Little rectification - timer (by default) change value 64 times per second. Only for check is this answer is correct, you can run with your program with setting timer period by calling timeBeginPeriod / timeEndPeriod. But please don't change windows timer on final version of your program. msdn.microsoft.com/en-us/library/windows/desktop/… – user1837009 Aug 7, 2013 at 16:12

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.