Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Noscript and fritz.box #dib0 http://t.co/zxHEedNf9Q
about 8 days ago Social engineering from India #dib0 http://t.co/ajjp43WkVS
about 17 days ago @unwoman Got the kickstarter Uncovered Vol. 2 today! Thank you! I love it! http://t.co/x0Tzovtq8u
about 19 days ago A query is running #dib0 http://t.co/cRZ8Dd3nVp
15 Apr 2013 Locally save attachments from Oulook using VBScript #dib0 http://t.co/l6RhWQsvFL
1 Apr 2013 Publishing Outlook calendar for use in Google calendar through http://t.co/sfh5eNxGXM #dib0 http://t.co/IzWNPlaqNA
21 Mar 2013 http://t.co/j3B0kSLGkM Really interesting article. The church of pirates. Gods preferential option for the poor in the broadest sense.
14 Mar 2013 Happy Pi-day! And this is what's wrong with it... funny, but true. http://t.co/A8GIB8fugC
14 Mar 2013 Hey guys @piwik ! Just looking at my site stats. I love the new page overlay feature. Well done! :-)
3 Mar 2013 Really funny! The Burning Hearts Revolution: How Sesame Street is Undermining Biblical Values http://t.co/z8XFk5P4d3
26 Feb 2013 Recursively check and correct mp3 files in Linux #dib0 http://t.co/U3nzOuWzWM
26 Feb 2013 Haha! Met zo'n antwoord een terechte reactie! http://t.co/NYXIb27aP5 via @snippers
20 Feb 2013 Create random password with C#, Java and PHP #dib0 http://t.co/WgF7DtcT
Home Architecture, security and coding Are break-statements (in Java and .Net) as bad as taught to me?
Are break-statements (in Java and .Net) as bad as taught to me?
Written by Division by Zero   
Thursday, 31 December 2009 15:17
Back in my days as a student I learned tha using the break statement is bad programming. Of course using the break statement within a switch statement is an exception to this rule. Since then I became a C# developer and I come across a lot of break statements in code of other developers. After a lot of (fun and usefull) discussion about this, it's time to put it to the test.

One of the reasons why it's a bad coding practice is that the break statement messes up the the branch prediction of the processor. While this is probably true for languages like C++, the question is if it is true for Java and C#, which are not directly compiled to assembly. I used these two methods to test if a break statement is actually slower than not using a break.
private const int totalAmountOfNumbers = 1000000;
private static int[] numbers;
private static Random random = new Random((int)DateTime.Now.Ticks);

private static bool DoSomethingWithBreak(int numberToFind)
{
 bool found = false;

 foreach (int number in numbers)
 {
 found = (number == numberToFind);
 
 if (found)
 break;
 }

 return found;
}

private static bool DoSomethingWithoutBreak(int numberToFind)
{
 bool found = false;

 for (int i = 0; (i < numbers.Length) && (!found); i++)
 {
 found = (numbers[i] == numberToFind);
 }

 return found;
}


Compiled to IL code these methods look like this.
.method private hidebysig static bool  DoSomethingWithBreak(int32 numberToFind) cil managed
{
 // Code size       36 (0x24)
 .maxstack  2
 .locals init ([0] bool found,
 [1] int32 number,
 [2] int32[] CS$6$0000,
 [3] int32 CS$7$0001)
 IL_0000:  ldc.i4.0
 IL_0001:  stloc.0
 IL_0002:  ldsfld     int32[] ConsoleApplication1.Program::numbers
 IL_0007:  stloc.2
 IL_0008:  ldc.i4.0
 IL_0009:  stloc.3
 IL_000a:  br.s       IL_001c
 IL_000c:  ldloc.2
 IL_000d:  ldloc.3
 IL_000e:  ldelem.i4
 IL_000f:  stloc.1
 IL_0010:  ldloc.1
 IL_0011:  ldarg.0
 IL_0012:  ceq
 IL_0014:  stloc.0
 IL_0015:  ldloc.0
 IL_0016:  brtrue.s   IL_0022
 IL_0018:  ldloc.3
 IL_0019:  ldc.i4.1
 IL_001a:  add
 IL_001b:  stloc.3
 IL_001c:  ldloc.3
 IL_001d:  ldloc.2
 IL_001e:  ldlen
 IL_001f:  conv.i4
 IL_0020:  blt.s      IL_000c
 IL_0022:  ldloc.0
 IL_0023:  ret
} // end of method Program::DoSomethingWithBreak

.method private hidebysig static bool  DoSomethingWithoutBreak(int32 numberToFind) cil managed
{
 // Code size       36 (0x24)
 .maxstack  2
 .locals init ([0] bool found,
 [1] int32 i)
 IL_0000:  ldc.i4.0
 IL_0001:  stloc.0
 IL_0002:  ldc.i4.0
 IL_0003:  stloc.1
 IL_0004:  br.s       IL_0015
 IL_0006:  ldsfld     int32[] ConsoleApplication1.Program::numbers
 IL_000b:  ldloc.1
 IL_000c:  ldelem.i4
 IL_000d:  ldarg.0
 IL_000e:  ceq
 IL_0010:  stloc.0
 IL_0011:  ldloc.1
 IL_0012:  ldc.i4.1
 IL_0013:  add
 IL_0014:  stloc.1
 IL_0015:  ldloc.1
 IL_0016:  ldsfld     int32[] ConsoleApplication1.Program::numbers
 IL_001b:  ldlen
 IL_001c:  conv.i4
 IL_001d:  bge.s      IL_0022
 IL_001f:  ldloc.0
 IL_0020:  brfalse.s  IL_0006
 IL_0022:  ldloc.0
 IL_0023:  ret
} // end of method Program::DoSomethingWithoutBreak


So the foreach and the break statements generate more IL code, but this says nothing about the performance of the code. After running the methods a few thousend times and calculating the average execution time, the method with break took an average of 0.04 milliseconds and the method without the break took 0.088 milliseconds.

So... the break is faster, so the branch prediction argument doesn't hold. Are there more arguments for not using the break statement? Actually, none that I can think of. It's a readable statement (if the rest of the code is readable!). I stand corrected!
Tags:
 

Add comment


Security code
Refresh

The right word may be effective, but no word was ever as effective as a rightly timed pause. - Mark Twain


© 2009 - 2013, Division by Zero

Template based on the empire template by joomlashack 

 Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.