Basicly I'd like to know what was the average CPU usage % for any piece of my perl code (for example while loop).

Your question makes no sense. Either your program has the cpu and is using 100% of it; or it doesn't and it is using 0%.

And it switches between those two circumstances many times a second depending upon a variety of factors almost none of which have anything to do with your program.

For example, if there are (say) five other processes running on your (single cpu) system -- and all six are totally cpu-bound (ie.do no I/O) -- and all six processes are running with equal priority, then the most cpu time your process can expect to get is 1/6th or 16.67%.

Now assume that two of those other processes do occasional I/O: then for the short period whilst those two processes are waiting for their IO requests to complete, your process has the potential to get 25% of the CPU.

Now assume that there is a 7th process running that only wakes up and does something every 10 seconds; but when it does, it runs with a higher priority than all the others; then for the period whilst that 7th process is running, your process will likely get 0% cpu.

If you add in multiple cpus as is the norm these days; and the fact that most OSs have dozens of system processes, running at various priorities, running all the time, and things change again.

And they change from millisecond to millisecond, for the entire runtime of your program. Or even your while loop.

The bottom line is that even if you could formulate and implement some mechanism for measuring the "average CPU usage % for any piece of my perl code", it would change every time you ran the program; and would therefore be a completely meaningless statistic that would tell you nothing useful about your code.

So, having got that out of the way; what were you hoping to achieve with the measurement? Ie. What is the real problem you are trying to solve?


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Re: CPU Monitor by BrowserUk
in thread CPU Monitor by siema24

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.