David Connell

Software Developer - Red Gate Software

Performance and Multiple Assigment in C#

Published Wednesday, January 25, 2006 5:13 PM

I was recently thinking if I really liked to use the Multiple assignment in 'C#' or if it was less readable. In 'C' this syntax was popular as it tended to lead to smaller and quicker code.
So I wondered in C# if the multiple assigment also lead to quicker code. So I quickly wrote some test cases to see which style of code was quicker to execute...

I took a rather simplistic model to profile, in order to make the tests more repeatable and easy to follow.

The .NET code followed the followed the structure:
[STAThread] static void Main(string[] args) 
            int Count1;
            int  Count2;
            int  Count3;
            int  Count4;
            int  Count5;
            int  Count6;
            int  Count7;
            int  Count8;

            Count1 =
            Count2 =
            Count3 =
            Count4 =
            Count5 =
            Count6 =
            Count7 =
            Count8 = 100;
            
			
            DateTime time = DateTime.Now;
            for (long i = 0; i < 1000000000; i++)
            {
                int  x = 100;
                Count1 = 
                Count2 = 
                Count3 = 
                Count4 = 
                Count5 = 
                Count6 = 
                Count7 = 
                Count8 = x;
            }

            TimeSpan span = DateTime.Now - time;
            Console.WriteLine("Time taken{0}", span.TotalMilliseconds / 1000.0);
} 

Test .NET 1.1
Debug Mode
.NET 1.1
under the debugger
.NET 1.1
Release Mode
Count1=x;Count2=x;..... 7 7 4.5
Count1=Count2=Count3 ...=x; 9.5 9.5 4.5
 
Results are in seconds
I did test out directly setting the values eg Count1=100; Count2=100; etc as well as using strings. These variations appeared to give similar results. I carried out the same expermiment under .NET 2 and the results were similar just slightly slower in debug mode.
The machine that carried out the tests was an Intel 3.4Ghz P4 running XP SP2 with 1GB RAM.


I guess as ever this proves that it is more important to write readable code that requires the minimum of maintanance, rather than trying to outwit the compiler/CLR.
So would I use the multiple assignment? Probably not as much as I used to, but when it leads to easier to read code then yes.

Comments

 

Salman said:

It's a developers dream come true if we can write both readable code whilst being performant.
March 7, 2006 1:40 PM
You need to sign in to comment on this blog

















<January 2006>
SuMoTuWeThFrSa
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234
Go With the Flow
 Knowing enough about the routes that messages take is vital to being an effective Exchange admin,... Read more...

When Email Collaboration Could Have Changed History
 In our mission to make history relevant to the busy IT executive, we speculate how Email might have... Read more...

Bunnikins!
 When an IT manager is selected as a victim of office politics of a large corporate, it is time for him... Read more...

Exchange Database Technologies
 One of the most misunderstood technologies in Exchange Server, regardless of its version, is the... Read more...

Top Tips for Exchange Admins
 Michael Francis hands out imaginary Olympic medals to the winner of the August 'Top Tips for Exchange... Read more...