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?

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

«Oldest   ‹Older   201 – 226 of 226   Newer›   Newest»
  1. Nice blog. Very informative. Having trouble solving different types of essays? Do not Worry. Our team of great essay writers is available 24 * 4 to help you with your needs. If each type of essay has different recommendations, more than one type of essay can be a little busy and confusing that we can understand well. However, this is not a puzzle for our exceptional essay writing assistants. They can deliver the compulsory essay writing help regardless of the depth of the subject or the type of essay.

  2. Best Assignment Experts are providing All Assignment Experts at affordable price. they provide verity of subject assignment help...before the deadline they are one of the best Assignment service provider site across the globe.

  3. Cool blog! Is your theme custom made or did you download it from somewhere? A design like yours with a few simple tweaks would really make my blog stand out. Please let me know where you got your theme. Thanks a lot
    AOL Customer Service Email

  4. Thumbs up for your excellent blog. Tents are used as habitation by nomads, recreational campers, soldiers, and disaster victims. Tents are also typically used as overhead shelters for festivals, weddings, backyard parties, major corporate events, excavation (construction) covers, and industrial shelters. Read more on Camping Tools List

  5. The academic writing process is the hardest part of paper creating, though composing quality content by oneself in this social bounded life is not achievable. MyAssignmentHelp provide online services for best academic writing, essay and plagiarized free content.

  6. To help students out and ensure a good academic career, my assignment experts has come forward to deliver high-quality assignment help services to students from every corner of the world. My Assignment Experts

  7. My Assignment Help offers affordable and reliable assignment help to students across Australia. We will provide proper guidance related to the assignment and facility among the students. We provide 100% plagiarism free work and the task will be completed within the time limit.

  8. The American Psychological Association reference style is one of the Author-Date reference styles.We provide you best harvard apa referencing.

  9. The well-written post represents how you have put most of your focus into including complementary information. I found it extremely insightful. If you are looking for something to aspire your academic journey, you should definitely try out the proficient Essay Writing Help service by the LiveWebTutors platform. The comprehensive solutions prepared by ingenious experts will definitely help you to get a better insight into your subject area.

  10. How Our PhD. Experts Help Students To Rewriting Their Essay, who dont know how to write an essay

  11. Our Independent call girls offers high profile Escort Service in goa at cheap rates. Graduated & Model independent Russian call girls in Goa.

  12. These factors, in confluence with the rising demand for energy, are acting as major growth-inducing factors bolstering the Floating Production Storage and Offloading Market growth.


  13. When it comes to providing best assignment service online,
    Unique Submission makes sure to complete assignment antecedently to offer scholars enough time to proof read the given assignment
    before submitting it to their tutors/professors. We never compromise to timely delivery and deadlines given by our customers.

    Case Study Writing Help

  14. Well, Now you need not to worry about your pending assignment writing.Native assignment help is one of the best assignment service provider in UK. We are here to give you best assignment help UK based service. Browse our services of HND Assignment Help, Edinburgh, Manchester and Across UK.

  15. Excellent reminder on the importance of submitting premium quality assignments to your professors and mentors. This post is really helpful for the students seeking professional help with their academic assignments in Australia. Sometimes the deadline is only two hours away and they need urgent writing assistance to get their hands on a flawless homework solution and this is where the Homework Help service by the LiveWebTutors platform comes into the picture. Try their writing facilities and experience the advantages associated with swift turnarounds.

  16. Are you a Business student? Are you doing a study in Strategy Assignment help? Are you assigned with lengthy and Corporate assignment help? Are you not willing to complete your Strategy management assignment help? Are you not able to understand complicated calculations and difficult queries? These are the reason you not be able to complete your Business law assignment help and need an expert for your help. If you are looking to score well in your final projects then you are at the right place. You can get undoubtedly get the best professional experts for all your Corporate law assignment help online problems.

  17. The Programming Homework Help
    expert went out of the way and did even more than I asked him to do. I've fallen for his rich knowledge of Java and how he perfectly organizes his solutions for the student's easy revision. It's never easy to bump into such genius Java Homework Help
    specialists that charge only a few bucks to get the job done perfectly.

  18. Hey there, can someone with Statistics Homework Help with the analysis of covariance? But I do not know how to conduct the ANCOVA test in SPSS. But that is not it. I don't really know how the test is done even manually. I know that there are two kinds of ANCOVA tests. The one-way ANCOVA and the two-way ANCOVA. As a STATA homework help expert if you can help me answer both the assignments, then I will hire you for my Statistics Assignment Help.

  19. Most of the Matlab Homework Help experts are very passionate about the service that they offer here. I can quickly tell it from their solutions. They're determined to write clear solutions that are easy to understand even by programming rookies. What's more, these solutions are always accurate and free from plagiarism issues. Thanks for Matlab Assignment Help for winning grades. They made me the best in my class.

  20. How do I know that you are going to offer me quality economics homework help? I am just so afraid of these online platforms. I know some are genuine but the problem is proving the genuine ones and the ones that are not genuine. Can I see your economics assignment help past work? I think that would help me know the type of expert you are.


  21. Your opinion is very good, I find it very interesting. The Post is informational and always produce wondrous things. Thank you for sharing with us.

    Also if you are looking forward to learn about wireless microphone problems in events and its solution then visit the article, name Solutions For Wireless Microphone System Problems For Event Organizer. . This article will guide you towards precautions to take when using wireless mics on event.

    Also I am sharing an article link based on best typing tools to practice typing daily. If you need to learn more about this then go through the full article 15 Free Typing Test Tool To Test Typing Speed Online.

  22. The Programming Assignment Help experts impressed me in many ways. They agreed to revise the paper without demanding more than the original order cost. That was very professional. The C++ Homework Help tutors also promised me an A and fulfilled it! I've never worked with such a cooperative team in my programming career. I'll continue to order from them each time I need a past paper revision or assignment help services.

  23. As a SPSS homework help expert can you tell me what really is the multivariate analysis of variance often abbreviated as MANOVA? What is the essence of this test in statistics? How is it related to SPSS? Obviously, there are a lot of questions that I can ask here. But you can be of help if you agree to Statistics Homework Help.I will pay for this so tell me how much you charge for this and Statistics Assignment Help in general.

  24. Can any one please get hourly tutoring rates? I need an experienced Australia Matlab Homework Help tutor that can take me through online Matlab Data Analysis classes for two hours each day for a week. To get adequately prepared for that, I need the price rates (or prospective rates) in prior. The Matlab Assignment Help tutor should be a Australian with sufficient experience in the relevant field. I hope you'll be more than happy to help me.

  25. Can you please tell me how much you charge for economics assignment help? I have gone through your website and it looks huge. With the rating I have seen there, I expect that your economics homework help tutors charge very high rates. Give me your rates so that I can know whether it is something I can afford.

  26. It is fact that Writing a conclusion is one of the challenging tasks, it becomes more difficult when it comes to sophisticated topic. In such cases you can have some help of the Conclusion Generator Online. You can check the detailed information of the same from here.

«Oldest ‹Older   201 – 226 of 226   Newer› Newest»

Post a Comment