{"id":2267,"date":"2006-01-25T17:13:00","date_gmt":"2006-01-25T17:13:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/performance-and-multiple-assigment-in-c\/"},"modified":"2016-07-28T10:48:45","modified_gmt":"2016-07-28T10:48:45","slug":"performance-and-multiple-assigment-in-c","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/performance-and-multiple-assigment-in-c\/","title":{"rendered":"Performance and Multiple Assigment in C#"},"content":{"rendered":"<p>I was recently thinking if I really liked to use the Multiple assignment in &#8216;C#&#8217; or if it was less readable. In &#8216;C&#8217; this syntax was popular as it tended to lead to smaller and quicker code.<br \/>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&#8230;<\/p>\n<p>I took a rather simplistic model to profile, in order to make the tests more repeatable and easy to follow.<\/p>\n<p>The .NET code followed the followed the structure:<br \/><code><\/code><\/p>\n<pre>[STAThread] static void Main(string[] args) \n            int Count1;\n            int  Count2;\n            int  Count3;\n            int  Count4;\n            int  Count5;\n            int  Count6;\n            int  Count7;\n            int  Count8;\n\n            Count1 =\n            Count2 =\n            Count3 =\n            Count4 =\n            Count5 =\n            Count6 =\n            Count7 =\n            Count8 = 100;\n            \n\t\t\t\n            DateTime time = DateTime.Now;\n            for (long i = 0; i &lt; 1000000000; i++)\n            {\n                int  x = 100;\n                Count1 = \n                Count2 = \n                Count3 = \n                Count4 = \n                Count5 = \n                Count6 = \n                Count7 = \n                Count8 = x;\n            }\n\n            TimeSpan span = DateTime.Now - time;\n            Console.WriteLine(\"Time taken{0}\", span.TotalMilliseconds \/ 1000.0);\n} <\/pre>\n<p><code><\/code><\/p>\n<table>\n<tbody>\n<tr>\n<td valign=\"top\">Test<\/td>\n<td valign=\"top\">.NET 1.1<br \/>Debug Mode<\/td>\n<td>.NET 1.1 <br \/>under the debugger<\/td>\n<td>.NET 1.1 <br \/>Release Mode<\/td>\n<\/tr>\n<tr>\n<td>Count1=x;Count2=x;&#8230;..<\/td>\n<td align=\"right\">7<\/td>\n<td align=\"right\">7<\/td>\n<td align=\"right\">4.5<\/td>\n<\/tr>\n<tr>\n<td>Count1=Count2=Count3 &#8230;=x;<\/td>\n<td align=\"right\">9.5<\/td>\n<td align=\"right\">9.5<\/td>\n<td align=\"right\">4.5<\/td>\n<\/tr>\n<tr>\n<td>&#160;<\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Results are in seconds<br \/>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. <br \/>The machine that carried out the tests was an Intel 3.4Ghz P4 running XP SP2 with 1GB RAM.<\/p>\n<p>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. <br \/>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was recently thinking if I really liked to use the Multiple assignment in &#8216;C#&#8217; or if it was less readable. In &#8216;C&#8217; 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&#8230;&hellip;<\/p>\n","protected":false},"author":48422,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2267","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2267","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/48422"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2267"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2267\/revisions"}],"predecessor-version":[{"id":41357,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2267\/revisions\/41357"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2267"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2267"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2267"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2267"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}