top of page

Are (Coded) Ram reads faster than ram writes?

  • Writer: Glen
    Glen
  • Jan 25, 2021
  • 2 min read

Updated: Jan 26, 2021


So I am writing a piece of speed-critical code (in C) when I come up with a familiar situation which I have never previously thought too much about.


I have a var that is initiated as 0, then running some code in a loop, only if a specific condition is met I then want to change that var to a 1 so I know later that the situation was met and something happened in that earlier loop. FYI, I need to process all records in the loop, I can't stop the first time the condition is hit, as I want to know later if the condition was met with at least one record).


The question is, is it faster to just update that var to a 1 everytime or to check first to see if it is still a 0 and only then update it.


Searching on google the main conclusion is that writes are slower than reads, so it would make sense that it is faster to check the current value and only update to 1 it is not although a 1, right?


But hold on, that would be two opps, first the var needs to be read from ram and then compared to see if it needs to be changed? As it is not just a read (its a read+check), is it still faster to check first and only update if needed?


I decided the only way to find out is to run a small test, I wrote two almost identical bits of code. (oh in case you are wondering why I'm using a structure, this is what I'm dealing with in the real code, not sure if addressing a struct is any slower than a plain var, so wanted to make sure this test matches what I am doing, so testing struct compared to plain var would a test for another day):


1) checkbeforeupdate

===

struct struct_gfsopenfilehandles

{

int updated;

};


int main()

{

struct struct_gfsopenfilehandles gfsfh;


long loop=0;

gfsfh.updated=0;


for ( loop=0; loop < 10000000000; loop++ )

{

if ( gfsfh.updated == 0 ) gfsfh.updated=1;

}

return 0;

}


2) justdoit

===

struct struct_gfsopenfilehandles

{

int updated;

};


int main()

{

struct struct_gfsopenfilehandles gfsfh;


long loop=0;

gfsfh.updated=0;


for ( loop=0; loop < 10000000000; loop++ )

{

gfsfh.updated=1;

}

return 0;

}


===


Now, for a quick and simple time test:


[glen@godev01 ~]$ time ./checkbeforeupdate


real 0m20.263s

user 0m20.245s

sys 0m0.001s

[glen@godev01 ~]$ time ./checkbeforeupdate


real 0m20.300s

user 0m20.282s

sys 0m0.001s

[glen@godev01 ~]$ time ./checkbeforeupdate


real 0m20.257s

user 0m20.240s

sys 0m0.002s



[glen@godev01 ~]$ time ./justdoit


real 0m17.369s

user 0m17.353s

sys 0m0.002s

[glen@godev01 ~]$ time ./justdoit


real 0m17.386s

user 0m17.372s

sys 0m0.001s

[glen@godev01 ~]$ time ./justdoit


real 0m17.367s

user 0m17.344s

sys 0m0.005s



So here we go, question answered, it really is faster to update the var everytime, than it is to check its contents first to see if it needs updating.


Moreover, with this testing overall, it is 15% faster, and for a speed-critical application, 15% is a huge win!


Of course, this does not say that memory writes are faster than reads, but it does say from a code perspective in this situation just doing writes is!


Ok note to self, back to work!

Comments


bottom of page