Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>1) you are currently creating 90000 threads, which is not efficient at all. Don't create every time a thread, use a thread pool instead, so you reuse threads that are already created. Remember creating a thread takes some time and memory.</p> <p>2) you are locking the whole code block with <code>lock</code>, which means each thread is blocked until another thread completed its work. So you basically are defeating the whole purpose of multi-threading here.</p> <p>3) Disk I/O doesn't work well with multi-threading for complex hardware-related reasons (buffers....etc.). Generally it's not a good idea to multi-thread this part of code.</p> <hr> <p>About comments concerning disk I/O and multi-threading : that's quite complicated actually. </p> <p>For magnetic disks, the disk arm has to move in order to read/write bytes on the good sector/cylinder/track. If you write 2 different files at the same time (case of two threads, each writing a different file), depending on the physical file location on disk, you may ask your disk arm to <strong>switch from one physical location to another very quickly</strong>, which destroys performances. Writing multiple disk sectors for the first file on one physical location, <strong>then</strong> moving the disk arm to another location, <strong>then</strong> writing some disks sectors for the second file would be much more efficient. You can see this effect when you compare the time to copy two files at the same time vs copying one file, then the other.</p> <p>So for this very basic example, performance gain/loss depends on:</p> <ul> <li>hardware itself. There is no disk arm with SSDs, so file access is way more faster</li> <li>physical file location</li> <li>file fragmentation</li> <li>bufferization. The <a href="http://en.wikipedia.org/wiki/Disk_buffer">disk buffer system</a> helps reading consecutive blocks, which could not be of any help in case you have to move the arm to another location.</li> </ul> <p>My humble advice: try to avoid multiple reads/writes in multiple threads if performances is your primary goal.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload