ISourceCode

Make the frequent cases fast and the rare case correct

C# – code optimization – Decimal value computation

public Decimal(
	int lo,
	int mid,
	int hi,
	bool isNegative,
	byte scale
)
//Fill in with any amount, no decimal present but implied (e.g. 500 represents $5.00)
var st = new Stopwatch();

st.Start();
 for (ulong ulongAmount = 10000000; ulongAmount < 20000000; ulongAmount++)
 {
 // Optimized ulongAmount / 100m
 decimal amount = new Decimal((int)ulongAmount, (int)(ulongAmount >> 32), 0, false, 2); 
 }
 st.Stop();
 st.Dump();

st.Reset();

 st.Start();
 for (ulong ulongAmount = 10000000; ulongAmount < 20000000; ulongAmount++)
 {
 decimal amount = ulongAmount / 100m;
 }
 st.Stop();
 st.Dump();

Image

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: