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?
In reply to Re: CPU Monitor
by BrowserUk
in thread CPU Monitor
by siema24
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |