-1

I wrote a tiny program that measures the time for c write() function in Linux. What is surprising to me is that the time are not consistent over multiple iterations. I have seen the first write takes much longer than the immediate consecutive writes. The pattern continues over multiple runs/filesize-buffersize combinations.

Here is the code: https://drive.google.com/file/d/1akwUz9mykkp0kk-FID9jxPClwJMD7uRI/view?usp=sharing

Here is a partial output:

it:0 filesize:4.00MB buffersize:1.00KB writeTime:0.051 S:MS

it:1 filesize:4.00MB buffersize:1.00KB writeTime:0.027 S:MS

it:2 filesize:4.00MB buffersize:1.00KB writeTime:0.022 S:MS

it:3 filesize:4.00MB buffersize:1.00KB writeTime:0.021 S:MS

it:4 filesize:4.00MB buffersize:1.00KB writeTime:0.021 S:MS

it:5 filesize:4.00MB buffersize:1.00KB writeTime:0.021 S:MS

it:6 filesize:4.00MB buffersize:1.00KB writeTime:0.022 S:MS

it:7 filesize:4.00MB buffersize:1.00KB writeTime:0.022 S:MS

it:8 filesize:4.00MB buffersize:1.00KB writeTime:0.021 S:MS

it:9 filesize:4.00MB buffersize:1.00KB writeTime:0.021 S:MS

Notice how write function call on it:0 took almost twice than consecutive calls. This pattern continues repeatedly. It may have something to do with linux page cache but I dont know how exactly its playing out underneath. Help!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847

1 Answers1

0

This has to do with the cache architecture of processors. During the first iteration of your program, the files stored in your hard drive or SSD memory are moved to the cache in order to be processed which takes more time with increasing file sizes. When the program needs the same file again, it will simply use the file stored in cache, skipping the trip to main memory. This creates the write time difference between the first and the rest of the iterations.

  • 1
    Cache hits can also hit at the OS level and, depending on the drive hardware, in the disk drive's electronics as well. – Gilbert Aug 07 '20 at 12:52