{"id":5105,"date":"2014-03-30T18:53:52","date_gmt":"2014-03-30T18:53:52","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-does-threading-save-time-collection-of-common-programming-errors\/"},"modified":"2014-03-30T18:53:52","modified_gmt":"2014-03-30T18:53:52","slug":"how-does-threading-save-time-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/how-does-threading-save-time-collection-of-common-programming-errors\/","title":{"rendered":"How does threading save time?-Collection of common programming errors"},"content":{"rendered":"<p>Most of the comments you have are correct, but I&#8217;ll throw my two cents out as well (and list the comments in here):<\/p>\n<p>Jonesy: &#8220;threading is most efficient in multi core environments&#8221; -&gt; Yes, but this is a single core cpu&#8230;so I&#8217;ll come back to this.<\/p>\n<p>KooKiz and John Sibly: They both mention I\/O. Your machine is not crunching along at full power 100% of the time. There are quite a lot of other things happening that take up time, and during those events, your CPU gets to take a break.<\/p>\n<p>(point of reference: I\/O can be network transmission, hard disk\/RAM read, SQL query, etc. Anything that brings in new data to the CPU or offloads data FROM the CPU)<\/p>\n<p>These breaks are time that your cpu can be doing other things. If you have a single core cpu (we&#8217;ll ignore hyperthreading at the moment), and a single-thread application, then it runs happy as can be. It does not, however, run constantly. CPU scheduling will give it a cycle or two, then move on to something else, then after a while come back to your program, give it a few more cycles, move on, etc. This gives the ILLUSION of being able to do &#8220;multiple things at once&#8221; on a single core cpu.<\/p>\n<p>Now, since this is a normal program and not some insanely small assembly program where you are writing values to the cache directly, your program stores data in RAM&#8230;a relatively slow storage medium compared to the CPU cache. Because of this, loading values takes time.<\/p>\n<p>During this time, it is possible that your CPU has nothing better to do. This is where you can see a speedup on a multi-threaded application, even on a single core. The other thread will fill in those extra CPU cycles, where the CPU would otherwise be idle.<\/p>\n<p>Note that it is highly unlikely that you will see a 2:1 speedup. It is much more likely that your 2-threaded program will only see a 10-20% speed boost, if that. Remember, the &#8220;other&#8221; thread (which at any given point is the thread that is NOT performing I\/O) will only really be running at its full capacity while the first thread is performing I\/O.<\/p>\n<p>Frequently, however, you can actually see a WORSE time. This is because your CPU now has to spend more time switching between the threads in your process (remember, we can only run one thing at a time!). This is called <em>overhead<\/em>. The second thread creates more overhead than it can make up for, and so the process overall slows down.<\/p>\n<p>On a multicore machine, you have two physical executors&#8230;which means the second thread gets an entire new core to work with. This means that it doesn&#8217;t have to compete with as many other things for execution time. Therefore, we get a substantial speedup here.<\/p>\n<p>Then of course you have multiprocess programs that execute over a cluster, but we&#8217;ll save that for another time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Most of the comments you have are correct, but I&#8217;ll throw my two cents out as well (and list the comments in here): Jonesy: &#8220;threading is most efficient in multi core environments&#8221; -&gt; Yes, but this is a single core cpu&#8230;so I&#8217;ll come back to this. KooKiz and John Sibly: They both mention I\/O. Your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5105","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=5105"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/5105\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=5105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=5105"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=5105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}