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!