Trivial Solution To CPU-Bound Thread Slowdown On Multicore Systems

According to David Beasley, the CPython GIL doesn't just prevent CPU-bound applications from taking advantage of multiple cores. He says it also slows them down on multicore systems.

As an example, he says, following CPU-bound function runs is slower on multicore systems when run in two threads vs when run twice sequentially on the same thread:
def counts(n=10000000):
while n > 0:
n -= 1

I tested the claim by running this little program (let's call it
from time import time
from threading import Thread

def counts(n=10000000):
while n > 0:
n -= 1

def measure(f, comment):
print time()-t, "seconds"

def sequ():

def para():
t1 = Thread(target=counts,args=())
t2 = Thread(target=counts,args=())
t1.join(); t2.join()

if __name__ == "__main__":
measure(sequ, "Sequential Execution")
measure(para, "Sequential Execution")

Here's the result from running the script on a Core Duo 2 laptop:
Sequential Execution: 4.81399989128 seconds
Parallel Execution: 11.2730000019 seconds

He's sort-of right in this case. So why has the GIL has survived? Partly because the problem is easily solved.

One way to do that is to use the sys.checkinterval function which allows CPU-bound threads to do a little more work before giving up control of the GIL. Since the overhead of GIL passing in the above is more than 100%, we need to increase the check interval by a factor of hundred or so. (The default is 100.)

Just add the following to after "if __name__ == '__main__'":
import sys

Let's run it again and see what happens:
Sequential Execution: 4.63999986649 seconds
Parallel Execution: 4.63199996948 seconds

Problem solved!

Avoiding Pre-emption Altogether

Personally, I like to include "sys.setcheckinterval(sys.maxint)" in all my scripts, so I don't have to lock shared data structures when manipulating them in Python code. If you do that, your threads will never be pre-empted as long as you don't call any blocking functions that release the GIL, and you can avoid the overhead of fine-grained locking, potential deadlocks, etc. What do you think?

40 comments :: Trivial Solution To CPU-Bound Thread Slowdown On Multicore Systems

  1. Forgive me if I'm wrong, but I'll mirror my reddit comment (since I don't want this to be google cached and lead some folks astray):

    Setting the check interval so high just means that they're effectively running sequentially... it's just running Thread1 until it's done and then Thread2 until it's done and voila... sequential time.

    With parallel execution you should get more than 0.008 seconds faster (which I would call a fluke).

  2. The above comment is correct. Also, time.time() measures wall clock time, so your benchmark times will be incorrect. You really want to measure CPU time, using time.clock()

  3. @TheMoken: 10000 ticks is as high as you think!

    In high performance mode, my core 2 duo runs 20 million iterations of the loop above in 2 seconds. That's 10 million iterations per second.

    Each iteration involves at least 3 bytecode instructions: decrement, compare, and jump. So that's at least 30 million python bytecode instructions per second.

    If the bytecode interpreter switches threads every 100 instructions, that means it's switching threads 300,000 times per second. That's ridiculous; no wonder it's more than twice as slow!

    Switching threads every 10000 instructions means switching threads 3000 times per second. I don't see how that's not enough to preserve the illusion of concurrent execution.

    I understand that for most real-world applications, each bytecode instruction is going to do much more than decrementing an integer counter, but then the slowdown on multicore systems will be correspondingly lower for those applications, too.

    If my deductions above are correct, then I think we can boldly say the GIL is not worse than what we thought it was, as long as the checkinterval is set appropriately.

    I think the default of switching every 100 bytecode instructions is extremely unrealistic in a world where systems have multiple cores and run millions of bytecode instructions per second.

  4. Correction:
    10,000 ticks is *NOT* as high as you think.

  5. It doesn't matter what you set it to, you're just closing in on sequential time by running them almost sequentially. Python's thread problems don't just go away because you start running larger block of the threads at a time. All that does is prevent the totally pointless threading overhead from choking your program. Basically, the threads are functioning in lockstep rather than in parallel, in addition to having the overhead of getting the GIL.

    If the system was properly threading, it would be a no-brainer to match sequential time but on your dual-core core2 the runtime should've dropped significantly because the native OS threads would've been able to take advantage of the multi-processor. That is... if Python's threading weren't fundamentally broken.

  6. Yes, CPython's threading is 'broken', but it's not as broken as the presentation suggested. It doesn't run more slowly on multi-core systems if configured appropriately.

  7. Seun, setting interval to 10,000 (3,000 per sec) will have the effect of limiting I/O per second, no?

  8. Peace be with you brother! i see your good works every where and i need your help in creating a forum similar to the one you've got.
    Agile Developers

  9. I like to get up early to go out and breathe fresh air. I feel that it is good for health and a good habit

  10. You’ve written nice post, I am gonna bookmark this page, thanks for info. I actually appreciate your own position and I will be sure to come back here.

  11. given article is very helpful and very useful for my admin, and pardon me permission to share articles

    here hopefully helped :


    menyembuhkan lambung bengkak

    Obat keputihan alami
    Penyebab usus buntu dan pengobatannya

  12. may be useful for all, helpful article once and pardon me permission to share also here Obat untuk menurunkan kolesterol tinggi

  13. This is for the programming gurus ✌✌✌. Visit my blog for cheats

  14. Thanks For Your valuable posting, it was very informative
    Webdesign LΓΌdenscheid

  15. Thanks for sharing. I hope it will be helpful for too many people that are searching for this topic.

  16. Wow.Such a wonderful post outlook tech support

  17. Hello every one i want to share my testimony on how i got my sugar-mummy true agent RAZAK )(08148752565), a friend of mine that always give me money…cos he was very rich, one day he said he is not going to give me fish that he is going to teach me how to fish, so i was very happy,never knew he have a SUGAR-MUMMY all my life will spend together! so i was CONNECTED with a RICH LOVING and CARING SUGAR-MUMMY, few days i was awarded a contract worth of millions, in my business i am doing very well right now,i am now the one that give money out before it was heard, if you want to be CONNECTED with a rich SUGAR-MUMMY or SUGAR-DADDY or LESBIAN-PARTNERS or GAY-PARTNERS contact [MAMAS LOVE-DATER BASE ORGANIZATION on 08148752565]FOR FAST HOOKUP

  18. Your post is really valuable, I'm a newbie in software development. I also design and manage several blogs like


  19. Your post is really valuable, I'm a newbie in software development. I also design and manage several blogs like


  20. Updat

  21. Excellent post. Thank you for sharing.
    ERP in Chennai



  24. πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£

    AM MR chuck's

    Am a crypto currency company official worker.
    Mine your bitcoin today. and earn bitcoin directly into your wallet daily
    πŸ•‘πŸ• πŸ•‘πŸ• πŸ•‘πŸ• πŸ•‘πŸ• πŸ•‘

    $1000 =EARN $826 DAILY
    $900=EARN $=713 DAILY
    $800=EARN$= 656 DAILY
    $700=EARN $=573 DAILY
    $600=EARN$=436 DAILY
    $500 = EARN=$368 DAILY
    $400=EARN= $284 DAILY
    $300=EARN= $142 DAILY
    $200=EARN= $71 DAILY
    $100=EARN =$35 DAILY





    +2349065242168 call or WhatsApp

    Remain bless
    Follow the link to the admin πŸ‘‡

  25. nice

  26. nice ...

  27. • Assignments are designed to make the students burn the midnight oil every day. Students experience depression, anxiety problems and undesirable stress due to so much work and so on. So they tend to move towards taking Assignment Help

  28. It is nice to read such high-quality content. It is a good article that discusses the topic at hand quite well. I am looking forward to read more articles from your site. Keep up the good work.
    assignment expert

  29. ABC Assignment Help is an incomparable online Accounting assignment help company delivering excellent academic assignments, essays, coursework and reports. Through a team of over 3000 subject experts we ensure individual attention to every student making the assignment help experience completely personalized in nature. With our round the clock services, you can be assured of high grades every time.

  30. In a competitive world, Assignment Writing Help me a lot with its effective and unique idea to present my work in an platable manner to score good marks. It provides a methodology to attract good marks in assignment. Thanks for your kind support to me.
    Assignment Help
    Assignment Help Online
    Online Assignment Help
    Assignment Help Online Services
    Assignment Helper
    Assignment Assistance
    Assignment Help Experts
    Online Assignment Help Services

  31. A research paper is something for that one has to do lots of studies and researchers to complete it. In the form of assignments, homework or papers, students have to prepare a research paper and they have submitted them to their faculties. It is a major part of which your grades are decided and shows your academic graph.

    If anyone thinks that a research paper is just a mere sum of sources on a topic, then you are surely wrong. It takes more than just writing a research paper if you want to score well. To get rid of these woes, hire our online research paper writers for quick research paper help from expert writers of My Assignment Help.

  32. Need cash fast? Get instant loan approval for payday loans in auburn at PaydayAlabama.Apply for instant payday loans and get same day approved. Just fill the online application form and get cash loan just by sitting in your home.For more information visit

  33. Well-explained blog! Writer has described the complete information of Assignment Help in detailed manner. Find all details regarding expert help at greatassignmenthelp to achieve high marks and to boost your academic performance.
    Assignment Help Online
    Online Assignment Help
    Assignment Help Online Services
    Assignment Helper
    Assignment Assistance
    Assignment Help Experts
    Online Assignment Help Services
    Assignment Writing Help

  34. A very high level post with a knowledgeable information .thanks you for giving me such a nice information. If you need any college level Assignment Help at reliable quality with better work. Kindly visit Ideal assignment help.

  35. Search for payday loans near me and get instant approval payday loans Denver. Apply for online payday loans Colorado Springs no credit check. Visit, a valuable service provider for your all financial needs.

Post a Comment