1

I have a Python application, running on Windows, that is somewhat CPU-intensive, and I'm trying to figure out the impact of some of my design choices.

Is there a way to measure CPU utilization for a particular process over a designated time period, both for total CPU, and on a per core basis? (per-core is important since Python's GIL prevents this program from using more than one core to any major extent, therefore for my 8-core PC, a CPU load of "only" 12% nearly maxes out one core)

I have Sysinternals Process Explorer, and it shows total CPU usage in 1-second intervals:

enter image description here

but the CPU usage bounces up and down, so I would like to use a longer measuring interval, essentially following this process:

  • start my Python application
  • configure my application (it has a UI) to run in a certain way
  • measure CPU usage over a moderately long interval (10-60 seconds)
  • configure my application in a different way
  • measure CPU usage over a moderately long interval (10-60 seconds)
  • repeat for additional configurations as needed
Jason S
  • 184,598
  • 164
  • 608
  • 970
  • It not about the measurement(s) you're seeking, but the references in [this answer](http://stackoverflow.com/questions/582336/how-can-you-profile-a-python-script/7693928#7693928) about how to profile a Python script might be useful to you. – martineau Jan 13 '15 at 23:37
  • Yeah, I've already looked at profiling to some extent; unfortunately I'm doing some evil stuff in Python and the profiler causes problems. So I'm really just looking for CPU measurements. – Jason S Jan 14 '15 at 00:14

1 Answers1

2

Take a look at the psutil libary for python?

You can use this function

psutil.pids()

To get a list of process id's, and then select a specific process this way (101 is arbitrary):

your_process = psutil.Process(101) 

You can also get CPU usage for this process:

your_process.cpu_percent(interval=1.0) 

You may be able to work these into a script to cater for your needs. I hope this helps, good luck!

Tom O' Mara
  • 267
  • 1
  • 2
  • 15
  • Thanks for the info -- unfortunately this is giving me erroneous results (at least compared to Process Explorer and System Explorer which seem to be consistent.); my Python app is taking about 7% of the CPU but psutil's `your_process.get_cpu_percent(interval=1.0)` is telling me around 25-30%. – Jason S Jan 14 '15 at 00:17