Is write to disk faster than write to memory? Don't think so

Not long after being featured in IT Worldthis paper went viral in Slashdot.

Code was written in different language: Java and Python and this paper claimed to prove that "in-memory DOES NOT always work faster than disk operation". I couldn't help but to read the code because it cannot be true.

First of all, the code is not proving that memory operation is slower than disk operation.

First part of the code repeatedly concatenate the String object "1" as a single byte using `concatString += addString` which is a proven inefficient way to do it (mentioned in every Java programming book). This is irrelevant with memory operation being slow.

Second of all, what "Single Write to Disk Time" does is actually not an in-memory operation. This is just the time used to write an object of a very big String (length of 1,000,000) into a file. What will happen is that CPU will fetch buffers of data from RAM and then pass it to the file.

The part "Write to Disk Time" that is claimed to be faster is the part where "1" is repeatedly written to BufferedWriter for 1,000,000 times. Of course it will be faster because "1" will be stored in CPU cache since it's used for 1 million times and fetching from CPU cache is very fast because the data is already there in the CPU.

It is not a surprise that reading "1" from CPU cache for 1,000,000 times is faster than reading a String containing 1,000,000 of "1"s from the RAM.

So, no, disk operation cannot be faster than memory operation.

OTOH this could also remind us how important it is to validate any research paper before citing or referring from it.

0 comments: (+add yours?)