Performance impact of the readonly keyword
Julien on Apr 22nd 2008
I've heard from a co-worker that the readonly keyword was optimizing memory access and therefore quicker than a normal field access. I must admit that I found this assertion a bit surprising. My first bet would have been that the performance are identical with or without it (it's just a compiler check, but it doesn't result in any change at execution). If wrong, I then would have bet on a slower access (I would expect the CLR to do some checks when accessing the field). Let's see if I'm mistaken :).
Unfortunately for me, I didn't find anything to confirm or not my theories, so I decided to do my own micro-benchmark.
I measured the time spent for 3 main scenarios, with a read only field and without it:
- Scenario 1: Creating an object
- Scenario 2: Accessing the field
- Scenario 3: Creating an object and accessing the field
I executed this code 1,000,000,000 times for each scenario and ran each scenario 10 times on 4 different PC to get a meaningful average. For each test, I excluded the top and bottom result.
The code (in C#, .NET 2.0) is available here (It is duplicated for each test, I know it's horrible, my apologies :)), and the results here.
Each test looks like that (with sligh modifications of course):
_streamWriter.WriteLine(); _streamWriter.WriteLine("Obj creation & access in loop"); for (int j = 0; j < _numberOfTestExecutions; j++) { watch.Start(); for (int i = 0; i < _numberOfIterationPerTest; i++) { Poco myCopyOfPoco = objReadOnlyField.Poco; } watch.Stop(); _streamWriter.Write(watch.ElapsedMilliseconds + ";"); watch.Reset(); watch.Start(); for (int i = 0; i < _numberOfIterationPerTest; i++) { Poco myCopyOfPoco = objReadOnlyField.Poco; } watch.Stop(); _streamWriter.WriteLine(watch.ElapsedMilliseconds); }
Here are the raw numbers:
Scenario 1: Creating an object
- read only: 25172ms
- read write: 24983ms
Read only is in average 0.76% slower than read write.
Scenario 2: Accessing the field
- read only: 6437ms
- read write: 6449ms
Read only is in average 0.19% quicker than read write.
Scenario 3: Creating an object and accessing the field
- read only: 19929ms
- read write: 19761ms
Read only is in average 0.85% slower than read write.
However, you can see greater differences by looking at the results for each computer. For instance, on the first PC I used (a core 2 duo, 2,8Ghz, DDR2 800Mhz), I have the following results:
- Scenario 1: Creating an object
Read only is in average 5.72% quicker than read write.
- Scenario 2: Accessing the field
Read only is in average 0.94% quicker than read write.
- Scenario 3: Creating an object and accessing the field
Read only is in average 2.28% quicker than read write.
And on a laptop (centrino 1,7ghz, DDR):
- Scenario 1: Creating an object
Read only is in average 6.34% slower than read write.
- Scenario 2: Accessing the field
Read only is in average 0.21% quicker than read write.
- Scenario 3: Creating an object and accessing the field
Read only is in average 3.01% slower than read write.
My (own) conclusions:
There seem to be a difference of performance involved by using the readonly keyword. The problem is that the impact is highly dependant on the hardware. On a PC with decent hardware, accessing a read only field is between 1% and 2.5% quicker than accessing a normal field. Object creation can be as much as 6% quicker. On lower-end hardware, the results are the opposite.
However, the tests have been made with very simple class, so keep in mind that in reality, the speed difference in object creation would have been smaller with classes that have more members.
As far as I'm concerned, I would not used the readonly keyword just for optimization purposes except for very demanding situations, and only after checking that it's actually quicker on the specifics machines where the software is going to run. So unless your application is:
- running on a very controlled environment with a very limited set of installations
- pseudo real-time or extremely sensible to performances
I don't think you should even wonder about the performance impact of readonly. There's probably hundreds of optimization that will be more effective before this one!
Finally, if you have any clue showing a different behaviour, please tell me about it and I will edit this post in consequence :). I'm specially interested in having benchmark with Xeon processors (unfortunately, I don't have access to one)
Filed in .NET | 6 responses so far
The .Net Frog is also available in french at:
Sam Apr 23rd 2008 at 10:55 pm 1
This is fascinating to me. I do this kind of micro-benchmarking too. It helps me understand what is really happening with the code. That is far more useful than those 200 milliseconds. Great post.
Fredrik Apr 24th 2008 at 12:38 pm 2
Although interresting to know about, to me the readonly keyword is more about semantics than performance. By making a field readonly, I’m explicitly saying that “this should never change”, and as such it is an important tool that should be used liberally. In enterprise development, the performance implications of readonly, virtual, interfaces and all that stuff is mostly neglible anyways. My two cents :)
Julien Apr 24th 2008 at 01:32 pm 3
I certainly agree that the semantic aspect should be the determining reason to use the readonly keyword in 99,99% of the cases, specially after the benchmark I ran. However, the readonly keyword is not absolutely needed (you can have a “readonly” behavior without using the keyword if you want to) which is not the case of interfaces or virtual members. Therefore, if you’re developping things that are extremely sensible to performances, I think it’s worth keepnig in mind that this “optional” keyword can have a small impact in both positive or negative ways. But as I said, given the benchmarks, I think it’s perfectly right to say that the semantics should drive your decision.
Dew Drop - April 24, 2008 | Alvin Ashcraft's Morning Dew Apr 24th 2008 at 04:04 pm 4
[...] Performance Impact of the readonly Keyword (Julien) [...]
Reflective Perspective - Chris Alcock » The Morning Brew #81 Apr 25th 2008 at 08:13 am 5
[...] Performance impact of the readonly keyword – A look at the performance difference obtained by using the readonly keyword [...]
James Curran Apr 25th 2008 at 05:08 pm 6
My general feeling is that “More information is better”, and so would use “readonly” when appropriate. Remember, just because the jitter doesn’t necessarily optimize for readonly now, doesn’t mean that it won’t in the future.
In think that jitter optimization could be the next big area of .Net innovation, where the .net framework installation process would choose one from perhaps a dozen different jitters, each customized to a particular CPU/platform, which would generate code which would run only a machine exactly like mine.