performance consideration when using properties multiple times
I am using CultureInfo.CurrentCulture
when formating my strings using string.format
To quote this blog
This just has the implication that if you are using CurrentCulture a lot, it might be worth reading it into a private variable rather than making lots of calls to CultureInfo.CurrentCulture, otherwise you're using up clock cycles needlessly.
so as per this author
var culture = CultureInfo.CurrentCulture
string.Format(culture,"{0} some format string","some args");
string.Format(culture,"{0} some format string","some other args");
is better than
string.Format(CultureInfo.CurrentCulture,"{0} some format string","some args");
string.Format(CultureInfo.CurrentCulture,"{0} some format string","some other args");
as per MSDN, CultureInfo.CurrentCulture is a property
Is there a performance penalty associated when accessing a property multiple times ??
Also I did some emperical analysis and my tests show me that using a local variable is more expensive than using the property directly.
Stopwatch watch = new Stopwatch();
int count = 100000000;
watch.Start();
for(int i=0;i<count;i++)
{
string.Format(CultureInfo.CurrentCulture, "{0} is my name", "ram");
}
watch.Stop();
//EDIT:Reset watch
watch.Reset();
Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine(watch.ElapsedTicks);
Console.WriteLine("--------------------");
var culture = CultureInfo.CurrentCulture;
watch.Start();
for (int i=0; i &l开发者_如何转开发t; count; i++)
{
string.Format(culture, "{0} is my name", "ram");
}
watch.Stop();
Console.WriteLine(watch.Elapsed);
Console.WriteLine(watch.ElapsedMilliseconds);
Console.WriteLine(watch.ElapsedTicks);
Result:
00:00:29.6116306
29611
68922550970
--------------------
00:00:27.3578116
27357
63676674390
My tests show that using CultureInfo.CurrentCulture
property is better than using local variable (which contradicts with the authors view). Or am I missing something here ?
Edit:I was not resetting the stopwatch before teh second iteration. hence the difference. resetting stopwatch, updating iteration count and result in this edit
The one true reason to rewrite your code to
var culture = CultureInfo.CurrentCulture;
String.Format(culture, "{0} some format string", "some args");
String.Format(culture, "{0} some format string", "some other args");
from
String.Format(CultureInfo.CurrentCulture, "{0} some format string", "some args");
String.Format(CultureInfo.CurrentCulture, "{0} some format string", "some other args");
is for readability and maintainability. Now, if you need for some reason to change the culture from CultureInfo.CurrentCulture
to a CultureInfo
that is loaded via some configuration file or passed in as a method to the parameter you only need to change the code in one place. Performance is a secondary consideration here and probably does not matter in that this is highly unlikely to be a bottleneck in your code.
You should only optimize CultureInfo.CurrentCulture into a local variable if a profiler shows it to be a significant problem in your code and also that putting into a local variable makes it faster. This profile shows neither is true so I would not put it into a local.
There is a bug in your code. In your test code you don't reset the Stopwatch. When you reset the stop watch, you'll see that using the cached reference is actually faster. CultureInfo.CurrentCulture isn't cheap, but string.Format is much more costlier.
精彩评论