2

Is it possible to check CPU usage of simple script ?

For example: How to get the CPU usage in % of printing 100 times "hello world!" ?

Currently I'm getting the execution time in the console, by:

time -p python script.py
3
  • so? what do you want that "time" don't let you? Commented Jun 19, 2017 at 18:51
  • Is stackoverflow.com/questions/15176619/… what you are looking for? Commented Jun 19, 2017 at 18:51
  • You can look at the top unix command. Commented Jun 19, 2017 at 19:02

3 Answers 3

3

If you are on a unix machine, you could always open top in a new terminal and then observe the % usage while you run your python program. Alternatively, there are some 3rd party libraries you can use.

Here's one: Benchmark

Examples (taken from the py package index).

Program:

from benchmarker import Benchmarker

## specify number of loop
with Benchmarker(1000*1000, width=20) as bench:
    s1, s2, s3, s4, s5 = "Haruhi", "Mikuru", "Yuki", "Itsuki", "Kyon"

    @bench(None)                ## empty loop
    def _(bm):
        for i in bm:
            pass

    @bench("join")
    def _(bm):
        for i in bm:
            sos = ''.join((s1, s2, s3, s4, s5))

    @bench("concat")
    def _(bm):
        for i in bm:
            sos = s1 + s2 + s3 + s4 + s5

    @bench("format")
    def _(bm):
        for i in bm:
            sos = '%s%s%s%s%s' % (s1, s2, s3, s4, s5)

Results:

$ python example.py -h              # show help
$ python example.py -o result.json
## benchmarker:         release 4.0.0 (for python)
## python version:      3.4.2
## python compiler:     GCC 4.8.2
## python platform:     Linux-3.13.0-36-generic-x86_64-with-debian-jessie-sid
## python executable:   /opt/vs/python/3.4.2/bin/python
## cpu model:           Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz  # 2494.050 MHz
## parameters:          loop=1000000, cycle=1, extra=0

##                        real    (total    = user    + sys)
(Empty)                 0.0236    0.0200    0.0200    0.0000
join                    0.2779    0.2800    0.2800    0.0000
concat                  0.3792    0.3800    0.3800    0.0000
format                  0.4233    0.4300    0.4300    0.0000

## Ranking                real
join                    0.2779  (100.0) ********************
concat                  0.3792  ( 73.3) ***************
format                  0.4233  ( 65.6) *************

## Matrix                 real    [01]    [02]    [03]
[01] join               0.2779   100.0   136.5   152.3
[02] concat             0.3792    73.3   100.0   111.6
[03] format             0.4233    65.6    89.6   100.0
Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide another example including the if __name__ == '__main__': part ?
2

You'll need the psutil module.

import psutil
print(psutil.cpu_percent())

3 Comments

psutil.cpu_percent is for the whole system, not an individual process. For just the current script, use current_process = psutil.Process(); print(current_process.cpu_percent()).
how to get cpu usage in each seconds
Complementary to @ErykSun answer. Warning the first time this method is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore. The second call is just fine :).
-1

This will not calculate the CPU usage, but the execution time. Use timeit and find the difference in the initial time of the execution and the end of the program. For example:

import timeit
start_time = timeit.default_timer()
print("Hello World")
print("Hello World")
print("Hello World")
end_time = timeit.default_timer()
print (end_time - start_time)

2 Comments

Question asked is not about finding execution time, Its about evaluating CPU utilization.
@MayurMahajan, Thanks, I just edited my answer to reflect what it actually does.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.