Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - new CultureInfo instantiation performance

I was wondering if it was a good practice to instantiate a CultureInfo object repeatdedly in a loop process (few thousand times). This object is required in many Date and String methods to force a specific culture when CurrentCulture may not be the right one.

var c = new CultureInfo("en-US", false);

What is the performance of a repeated instantiation?

like image 235
Vincent Avatar asked Sep 19 '13 18:09

Vincent


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

One would think that the optimizer in the C# and/or the JIT compilers would have the smarts to recognize a loop-invariant expression and refactor outside of the loop. My inclination is to do such refactorings myself as is makes the code clearer.

Even better, use this method:

CultureInfo ci = CultureInfo.GetCultureInfo("en-US") ;

It gives you a cached, read-only instance, the instance will only be constructed once and thence retrieve from cache.

Better yet, for your stated purposes:

This [CultureInfo] object is required in many Date and String methods to force a specific culture when CurrentCulture may not be the right one.

use CultureInfo.InvariantCulture. That is what it exists for.

A third option, would be to create a static CultureInfo property holding a singleton reference to your fallback culture. Depending on your purposed, you might want to mark it as thread-local (static methods of CultureInfo are thread-safe; instance methods are not). Such a propery might look something like this:

public static string FallbackCultureId { get { return Configuration.AppSettings["FallbackConfigurationId"] ; } }

public static CultureInfo FallbackCultureInfo
{
  get { return fallBackCultureInfo ?? (fallBackCultureInfo=new CultureInfo(FallbackCultureId)) ; }            
}
[ThreadStatic] private static CultureInfo fallBackCultureInfo ;
like image 54
Nicholas Carey Avatar answered Oct 03 '22 14:10

Nicholas Carey