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 gil.py)
from time import time
from threading import Thread

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

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

def sequ():
counts()
counts()

def para():
t1 = Thread(target=counts,args=())
t1.start()
t2 = Thread(target=counts,args=())
t2.start()
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:
C:\Py>python gil.py
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 gil.py after "if __name__ == '__main__'":
import sys
sys.setcheckinterval(100*100)


Let's run it again and see what happens:
C:\Py>python gil.py
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?

66 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
    19216811ll.com

  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.

    fnafgame.net

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

    here hopefully helped :

    Cara

    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.

    http://www.dexygames.club/parkour-games

  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 Abtechblog.com

    Thanks.

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

    Thanks.

  20. Updat

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

  22. https://api.whatsapp.com/send?phone=2349065242168&text=earn%20your%20bitcoin.%20sell%20and%20buy%20your%20BTC%20TBC%20ETC

  23. https://api.whatsapp.com/send?phone=2349065242168&text=earn%20your%20bitcoin.%20sell%20and%20buy%20your%20BTC%20TBC%20ETC

  24. πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£πŸ“£
    GOOD DAY ALL.
    CRYPTO CURRENCY, MINE YOUR BITCOIN TODAY AND LEAVE LIKE KING πŸ‘²πŸ½πŸ‘ΈπŸ½πŸ‘²πŸ½πŸ‘ΈπŸ½πŸ‘²πŸ½πŸ‘ΈπŸ½πŸ‘²πŸ½

    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

    NOTICE NOW, THE MORE YOU INVEST THE MORE YOUR CAPITAL IS HIGH..
    πŸ’²πŸ’²πŸ’²πŸ’²πŸ’²πŸ’²πŸ’²πŸ’²πŸ’²πŸ”œπŸ”œπŸ”œπŸ”œπŸ”œπŸ”œπŸ”œπŸ”œπŸ”œ

    YOU GAIN AND I ALSO GAIN☑☑☑☑☑☑☑

    INBOX ME FOR MORE INFOπŸ’¬πŸ’¬

    πŸ†“πŸ†“πŸ†“πŸ†“πŸ†“πŸ†“πŸ†“πŸ†“
    INVEST ON YOUR BLOCKCHAIN AND SMILE 😊 EVERY DAY..

    Contacts==odududave@gmail.com

    +2349065242168 call or WhatsApp

    Remain bless
    Follow the link to the admin πŸ‘‡
    https://api.whatsapp.com/send?phone=2349065242168&text=earn%20your%20bitcoin.%20sell%20and%20buy%20your%20BTC%20TBC%20ETC

  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 http://www.paydayalabama.com/

  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 coloradoloansnearme.com, a valuable service provider for your all financial needs.

  36. This post is very thoughtful and useful to me. I really like this and I’m waiting for your next post! MyAssignmentHelpAu providing best and high-quality Programming assignment help . You have simply visited our website and contact our expert team. Our experts available 24*7.

  37. Anyone can dial +1 800-684-5649 where our specialists will help you through the telephone call and help you in the greatest possible way so that you can simply enjoy as indicated by your work.

    How to connect hp printer to mac

    How to connect epson printer to wifi

    Connect mg3620 to wifi

    Canon mg2922 connect to wifi

    Hp officejet 4650 troubleshooting

    Canon Printer Customer Service Phone Number


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

  39. Livewebtutors aims at providing the students with a one stop solution for all the academic related issues. The company has Livewebtutors is an online Assignment Help and assignment writing service offering expert assignment help. Hire Australian assignment writing experts.
    visit now:- Nursing Assignment Help

  40. Nice work. Pls how can my pre wedding pictures be made public on Nairaland? Thanks

  41. Excellent information Providing by your Article, thank you for taking the time to share with us such a nice article. Amazing insight you have on this, it's nice to find a website that details so much information about different artists. Kindly visit livewebtutors website we providing best assignment help services in Australia.
    Read more now :- My Assignment Help

  42. Universities all around the United States assign CPM homework to students, who lack the basic mathematical skills in elementary colleges. Get cpm homework help online from us, in case you find any problem while solving the mathematical problems.

  43. This Is Really Great Work. Thank You For Sharing Such A Useful Information Here In The Blog.
    Paperssolution
    topacademictutors
    liveassignmenthelp
    studyhawks

  44. Excellent information Providing by your Article, thank you for taking the time to share with us such a nice article. Amazing insight you have on this, it's nice to find a website that details so much information about different artists. Kindly visit livewebtutors website we providing best assignment help services in Australia.
    Read more now :- My Assignment Help

  45. Are you looking for Payday Loans Florence Al? We are here to help to get payday loans quickly. Once approved, your payday loan will be credited to your account within 24 hours. Apply today!

  46. 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 livewebtutors website we providing best assignment help services in Australia, USA, UK, UAE, and Canada etc.
    Read more now :- Nursing Assignment Help

  47. Manage Payday Loan Repayment Even With Low Income

    Nobody likes borrowing money from family members or friends. for Payday Loans Colorado help you keep your identity discreet and also avoid cutting back further on your daily expenses to meet your loan requirements. There are many companies offering payday loans such as Payday Loans Alabama.

    Why Payday Loans Are Used By Borrowers?

    A Payday loan helps borrowers get some cast quickly in order to meet their unexpected expenses. Sometimes, a person might be in the need of a few hundred dollars only which can be easily met with a loan.

    You can avail loans from companies who have a large network. These companies refer your need to a quality payday lender. After the borrower fills the application form, it is sent to the payday lender to consider.

    How Do You Repay The Loan If You Have Low Income?

    There are many people who borrow online payday loans Denver but later struggle to repay the amount because they have a low income. But there are many payday loan lenders who agree to give cash loans to people needing some quick cash but are earning less. You may schdule your payment according to the payment terms. That will help you to repay the loan amount in best way!

    • for payday loans colorado no credit check are an easy alternative to getting some quick cash in case of emergency. Only requirement is to have a real job and a checking account.
    • In order to manage the loan repayment, it is best to chalk out your expenses and prioritize what is important for the next few months and cut down on unnecessary expenses until you repay all of your loan.
    • This is not very tough as far as you are dedicated to repay the loan amount completely and then live burden-free.
    • With low income, getting a payday loan is easy, but to repay it, you need to make some smart moves. You may take a second or a third loan if necessary, but you should have a well-devised plan on how you intend to repay. This is the beauty of a payday loan. You get instant money when nobody else is willing to help you and you only have to move smartly later to repay the loan amounts.
    • No other financial institutions or banks consider your low income while charging you rates of interest for the money borrowed, but with payday loans, the company connects you with only those lenders who charge a very low rate of interest. This situation is very beneficial to you and you can easily manage to repay the amount in a few days or months.

    A low income does not mean that you cannot get any cash loan in case of emergency. Only for payday loans Colorado Springs come to your rescue when you need that quick buck. You can also manage to repay the amount if you have your priorities set and will power to repay the loan amount first.

  48. This comment has been removed by the author.
  49. If you occur to find any trouble with your printer, troubleshoot by referring to resolutions of respected error. Not only hp envy 4520 troubleshooting but our technical experts can assist you to fix this failure by providing online remote help.

  50. Excellent information Providing by your Article, thank you for taking the time to share with us such a nice article. Amazing insight you have on this, it's nice to find a website that details so much information about different artists. Kindly visit livewebtutors website we providing best assignment help services in Australia.
    Read more now :- Business Assignment Help

  51. Assignments are a great way to make students understand the core concepts of the study. It makes them able to better understand the requirements for better management of finances once they start working after the studies. However, due to the limitations that students have, it is always advised to them to seek best assignment help rather than writing mediocre assignments and scoring poor grades because of that. Our all assignment help can help students to grasp the conceptual knowledge better and score perfect grades as well.

  52. Everything should be based on fact. The arrangement of fact and information is not an easy task sometimes it becomes a very tough task to do in such cases student can go through online Assignment Help , and these services are very reliable, and they can provide information about any topic related to the academics. If you need any college level Assignment Help at reliable quality with better work. Kindly visit our website.

  53. Thank you for this wonderful information looking forward for more. We are Providing the best quality assignment help. You will find out online My Assignment Help service on firstassignmenthelp. And these services are very reliable, and they can provide information about any topic related to the academics. If you need any college level Assignment Help at reliable quality with better work. Kindly visit our website firstassignmenthelp.

  54. Naija News Media is coming

  55. 'I'm highly impressed by the piece of thoughts you have shared on this portal. all the best
    connect us on Assignment Help can shed your burden of assignments with return of qualitative assignments.

  56. Rooting an Android device is the most important thing users look for after installing an Official Firmware,
    Install Z4root V1.4 Apk

  57. Thank you for this wonderful information looking forward for more. We are Providing the best quality assignment help. You will find out online My Assignment Help service on firstassignmenthelp. And these services are very reliable, and they can provide information about any topic related to the academics. If you need any college level Assignment Help at reliable quality with better work. Kindly visit our website firstassignmenthelp.

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

    Smash Hit

Post a Comment