Python Forum
CPU utilisation is confusing
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CPU utilisation is confusing
#1
I have a question regarding the CPU utilisation of Python. When running a calculation, the CPU usage for Python sits at about 30%, which I initially thought would mean that only one of my four cores was being fully used. After checking task manager, it seems that all four cores are being used but each at about 30%

How can I enable Python to use all available resources?

(would post images with this but it's my first post and I'm not allowed :/ sorry)
Reply
#2
All four cores at 25% is the same than one single core at 100% (I assume the extra 5% is other activity on your machine). Unfortunately, due to the GIL you cannot efficiently use all four cores simultaneously with Python (or at least with CPython which is the usual implementation...). So you have to either find a non GIL-limited Python, or spread your work load across processes instead of threads, which isn't too hard to do with the multiprocessing module.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#3
Surely the fact that it is using four cores mean that it's a multithreaded workload, but it's not using these threads effectively and I was wondering if that can be improved at all. 
[Image: HTuxSUL.png]

[Image: OwjXMG3.png]
Reply
#4
...show some code?
Reply
#5
I'm new to this stuff so it's nothing too impressive
import random
import math
coprime = 0
cofactor = 0
sets = 0
while(sets<10000):
    random1 = random.randint(1, 1000)
    random2 = random.randint(1, 1000)
    i = 2
    while(True):
        if i==random1 or i==random2:
            coprime = coprime + 1
            break
        elif random1%i==0 and random2%i==0:
            cofactor = cofactor + 1
            i = i + 1
            sets = sets + 1
(it's still running btw, any help with improving efficiency would be greatly appreciated but I'm ok with leaving this to run)
Reply
#6
(Mar-19-2017, 09:38 PM)Bidgey225 Wrote: Surely the fact that it is using four cores mean that it's a multithreaded workload,
You are jumping to conclusions. It could be a single thread that ends up being dispatched about equally over the available cores. The CPU percentage is an average over a period (one second) which is somewhat bigger than a dispatcher time slice (tens of milliseconds).
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#7
You have an infinite loop, though. If i is not equal to either random number, AND either of those numbers isn't divisible by i, your loop will never finish.
Which could explain why it's eating an entire core, without really doing anything.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python list out top 10 Memory utilisation mg24 0 800 Nov-20-2022, 10:51 PM
Last Post: mg24
  Confusing in [for loop] topic Sherine 11 3,497 Jul-31-2021, 02:53 PM
Last Post: deanhystad
  Confusing logic Blob 4 2,400 Nov-18-2019, 03:26 AM
Last Post: Blob
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,823 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  IndentationError message could be confusing to new programmers insearchofanswers87 1 2,342 May-16-2018, 05:05 PM
Last Post: Larz60+
  Confusing Math DrJu 2 3,233 Jan-18-2018, 10:47 PM
Last Post: Windspar
  Some Confusing Program Errors (Newbie stuff) WildPictus 1 2,786 Sep-03-2017, 05:00 PM
Last Post: hbknjr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020