- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
c# upc-a Java Example of a Loop That Can Be Unrolled in C#.NET
Java Example of a Loop That Can Be Unrolled EAN-13 Maker In Visual C# Using Barcode creator for .NET framework Control to generate, create EAN-13 image in Visual Studio .NET applications. www.OnBarcode.comScanning GTIN - 13 In Visual C#.NET Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comNormally, you d probably use a for loop for a job like this, but to optimize, you d have to convert to a while loop. For clarity, a while loop is shown here. Bar Code Encoder In Visual C# Using Barcode maker for .NET framework Control to generate, create bar code image in Visual Studio .NET applications. www.OnBarcode.comReading Bar Code In Visual C# Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.com} i = 0; while ( i < count ) { a[ i ] = i; i = i + 1; Draw EAN-13 In .NET Using Barcode generator for ASP.NET Control to generate, create GS1 - 13 image in ASP.NET applications. www.OnBarcode.comPrint EAN-13 Supplement 5 In VS .NET Using Barcode maker for VS .NET Control to generate, create EAN-13 Supplement 5 image in .NET applications. www.OnBarcode.comTo unroll the loop partially, you handle two or more cases in each pass through the loop instead of one. This unrolling hurts readability but doesn t hurt the generality of the loop. Here s the loop unrolled once: Make European Article Number 13 In Visual Basic .NET Using Barcode printer for Visual Studio .NET Control to generate, create EAN-13 image in VS .NET applications. www.OnBarcode.comCreating Code 128 Code Set B In C#.NET Using Barcode encoder for .NET Control to generate, create Code128 image in Visual Studio .NET applications. www.OnBarcode.comJava Example of a Loop That s Been Unrolled Once
Draw Linear 1D Barcode In C# Using Barcode maker for VS .NET Control to generate, create Linear image in .NET framework applications. www.OnBarcode.com2D Barcode Drawer In C#.NET Using Barcode drawer for .NET Control to generate, create Matrix Barcode image in .NET framework applications. www.OnBarcode.comi = 0; while ( i < count - 1 ) { a[ i ] = i; a[ i + 1 ] = i + 1; i = i + 2; } if ( i == count ) { a[ count - 1 ] = count - 1; } EAN-13 Printer In C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create EAN-13 image in .NET framework applications. www.OnBarcode.comDrawing Leitcode In C#.NET Using Barcode generator for .NET framework Control to generate, create Leitcode image in Visual Studio .NET applications. www.OnBarcode.comCODING HORROR
Draw Denso QR Bar Code In None Using Barcode creation for Online Control to generate, create QR-Code image in Online applications. www.OnBarcode.comMaking Code 3 Of 9 In Java Using Barcode drawer for Android Control to generate, create Code 3/9 image in Android applications. www.OnBarcode.com8 These lines pick up the case that might fall through the cracks if the loop went by twos instead of by ones. Printing PDF417 In None Using Barcode creation for Font Control to generate, create PDF 417 image in Font applications. www.OnBarcode.comDraw GS1-128 In VB.NET Using Barcode printer for Visual Studio .NET Control to generate, create USS-128 image in .NET framework applications. www.OnBarcode.comThe technique replaced the original a[ i ] = i line with two lines, and i is incremented by 2 rather than by 1. The extra code after the while loop is needed when count is odd and the loop has one iteration left after the loop terminates. When five lines of straightforward code expand to nine lines of tricky code, the code becomes harder to read and maintain. Except for the gain in speed, its quality is poor. Part of any design discipline, however, is making necessary trade-offs. So, even though a particular technique generally represents poor coding practice, specific circumstances may make it the best one to use. Here are the results of unrolling the loop: Data Matrix Creator In VS .NET Using Barcode generator for VS .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications. www.OnBarcode.comData Matrix 2d Barcode Maker In None Using Barcode creation for Font Control to generate, create DataMatrix image in Font applications. www.OnBarcode.comde Complete
Draw Barcode In .NET Using Barcode encoder for ASP.NET Control to generate, create bar code image in ASP.NET applications. www.OnBarcode.comEAN-13 Maker In Java Using Barcode maker for Java Control to generate, create GTIN - 13 image in Java applications. www.OnBarcode.com26. Code-Tuning Techniques
Page 12
Language C++ Java PHP Python
Straight Time 1.75 1.01 5.33 2.51
Code-Tuned Time 1.15 0.581 4.49 3.21
Time Savings 34% 43% 16% -27% Note: Benchmarked for the case in which count equals 100.
A gain of 16 to 43 percent is respectable, although again you have to watch out for hurting performance, as the Python benchmark shows. The main hazard of loop unrolling is an off-by-one error in the code after the loop that picks up the last case. What if you unroll the loop even further, going for two or more unrollings Do you get more benefit Here s the code for a loop unrolled twice: CODING HORROR
Java Example of a Loop That s Been Unrolled Twice
i = 0; while ( i < count - 2 ) { a[ i ] = i; a[ i + 1 ] = i+1; a[ i + 2 ] = i+2; i = i + 3; } if ( i <= count - 1 ) { a[ count - 1 ] = count - 1; } if ( i == count - 2 ) { a[ count -2 ] = count - 2; } Here are the results of unrolling the loop the second time: Language Straight Time 1.75 1.01 5.33 2.51 Single Unrolled Time 1.15 0.581 4.49 3.21 Double Unrolled Time 1.01 0.581 3.70 2.79 Time Savings 42% 43% 31% -12% C++ Java PHP Python
Note: Benchmarked for the case in which count equals 100.
The results indicate that further loop unrolling can result in further time savings, but not necessarily so, as the Java measurement shows. The main concern is how de Complete
26. Code-Tuning Techniques
Page 13
Byzantine your code becomes. When you look at the code above, you might not think it looks incredibly complicated, but when you realize that it started life a couple of pages ago as a five-line loop, you can appreciate the trade-off between performance and readability. Minimizing the Work Inside Loops
One key to writing effective loops is to minimize the work done inside a loop. If you can evaluate a statement or part of a statement outside a loop so that only the result is used inside the loop, do so. It s good programming practice, and, in some cases, it improves readability. Suppose you have a complicated pointer expression inside a hot loop that looks like this:
|
|