Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 15

LOB Mobile Application Development Workshop Lab Manuals

Lab: LAB06
Lab Manual Performance Tests

Summary
In this lab, youll build an application with several performance tests, so you can see the real impact of code changes in a mobile environment.

Estimated Time to Complete This Lab


45 minutes

Applies to
Microsoft Visual Studio 2008 Microsoft Windows Mobile 6.0 SDK Standard and Professional

Last Update
December 13, 2007

This is a preliminary document and may be changed substantially prior to final commercial release of the software described herein. The information contained in this document represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication. This White Paper is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred. 2006 Microsoft Corporation. All rights reserved. The names of actual companies and products mentioned herein may be the trademarks of their respective owners.

LAB 06: Performance Tests


After completing this lab, you will be able to: Determine which alternatives are best to build faster mobile applications

Performance Tests Application


This exercise is about a mobile application with some examples of coding alternatives in which you will test the performance impact in a mobile environment.

1. Open the startup solution. Build it (CTRL+SHIFT+B) and run the first Dummy Test.

2. To run the Strings vs. StringBuilder test, first copy this method and then paste it into
the MainForm class.
private void StringTests(bool Optimized, int RepeatCount) { long start, stop; int ix = 0; if (Optimized == false) { //No Optimized string Result = string.Empty; Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// ////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { //String concatenation using simple strings Result += "Hello World"; } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } else {

//////////

//Optimized StringBuilder sb = new StringBuilder();

//Very optimized StringBuilder instantiation with internal size pre-allocated (Not common as we hope) //StringBuilder sb = new StringBuilder("Hello World".Length * RepeatCount); string Result = string.Empty; Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { //String concatenation using StringBuilder sb.Append("Hello World"); } Result = sb.ToString(); stop = PerformanceHelper.GetPerformanceCount();

//////////

//////////////////////////////////////////////// ////////// } } Cursor.Current = Cursors.Default; DisplayResults(start, stop);

Then, uncomment the call to this new StringTests method youve just pasted, in the menuStrings_Click event handler.

3. Run the String vs. StringBuilder test 5 times with the Optimize option unchecked, and 5 times with the Optimize option checked. Write down the results here, and take a mean value. # Non Optimized Optimized 1 2 3 4 5 Mean

4. Now, comment the first empty initialization of the StringBuilder sb variable in the
Optimized test, and uncomment the more optimized version, which allocates all the space needed at start.

5. Run the optimized tests 5 times again, and write down the results here. # 1 2 3 4 5 Mean Non Optimized Optimized Very Optimized

6. To run the Boxing / Unboxing test, first copy and paste this method on the MainForm
class.
private void BoxingUnboxingTests(bool Optimized, int RepeatCount) { long start, stop; int ix = 0; if (Optimized == false) { //No Optimized Cursor.Current = Cursors.WaitCursor; object o = null; int i, j; //////////////////////////////////////////////// ////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { i = 123; // Initial variable assignation. o = i; // Implicit boxing (The value type is converted to object type) i = 456; // Change the contents of i j = (int)o; // Unboxing (may throw an exception if types are incompatible) } } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } else {

//////////

//////////

//Optimized Cursor.Current = Cursors.WaitCursor; int i, j; //////////////////////////////////////////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { i = 123; // Initial variable

assignation.

i = 456; // Change the contents of i j = i; // Compatible types (No automatic boxing/unboxing performed) } stop = PerformanceHelper.GetPerformanceCount();

//////////////////////////////////////////////// ////////// } } Cursor.Current = Cursors.Default; DisplayResults(start, stop);

Then, uncomment the call to this new BoxingUnboxingTests method youve just pasted, in the menuBoxing_Click event handler.

7. Now run the Boxing/Unboxing test 5 times, and write down the results here, and take a mean value. # 1 2 3 4 5 Mean the MainForm class.
private void ExceptionsErrorCodesTests(bool Optimized, int RepeatCount) { long start, stop; int ix = 0; if (Optimized == false) { //No Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// ////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { try { OperationThatReturnsException();

Non Optimized

Optimized

8. To run the Exceptions vs. Error Codes test, first copy and paste this three methods in

} catch { //Error processing code detected using try/catch } } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// ////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } else { //Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// ////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { try { if (OperationThatReturnsErrorCodes() == { //Error processing code detected using error code } } catch { //Error processing code detected using try/catch } } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } }

-1)

//////////

private int OperationThatReturnsErrorCodes() { return (-1); } private int OperationThatReturnsException() { throw new Exception(); }

10

Then, uncomment the call to this new ExceptionErrorCodesTest method youve just pasted, in the menuExceptions_Click event handler.

9. Now run the Exceptions vs. ErrorCodes test 5 times, and write down the results here, and take a mean value. # 1 2 3 4 5 Mean class.
private void ForForEachTests(bool Optimized, int RepeatCount) { long start, stop; int ix = 0; int result = 0; //Create one ArrayList and inserts 100 elements to perform the test ArrayList List = new ArrayList(); for (int aux = 0; aux < 100; aux++) { List.Add(aux); } if (Optimized == false) { //No Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// ////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { foreach (object value in List) { result += (int)value; }

Non Optimized

Optimized

10. To run the ForEach vs. For test, first copy and paste this method into the MainForm

11

} stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } else {

//////////

//Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// start = PerformanceHelper.GetPerformanceCount();

//////////

for (; ix < RepeatCount; ix++) { int count = List.Count; for (int j = 0; j < count; j++) // or for (int j = 0; j < List.Count; j++) { result += (int)List[j]; } } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// ////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } }

Then, uncomment the call to the ForForEachTests method youve just pasted, in the menuForEach_Click event handler.

11. Now run the ForEach vs. For test 5 times, and write down the results here, and take a mean value. # Non Optimized Optimized 1 2 3 4 5 Mean

12

12. To run the Stack vs. Heap test, first copy and paste this method in the MainForm class.
class ClassObject { public int x; } struct StructObject { public int x; } private static void PerformOperationOnClassObject(ClassObject c, int value) { //keep in mind that c is "a reference" (a pointer to) the original object. //When we update the value of x, we are updating the value of the object passed by reference to this method. //This results in that the caller can see the value changed inside of this method. c.x = value; } private static void PerformOperationOnStructObject(StructObject s, int value) { //keep in mind that s is "a copy" of the structure passed by parameter. //When we update this value, we are updating only the copy and no the original structure. //When this method goes out of scope this value is lost because we are not returning them to the caller. //This results in that the caller cannot see the value changed inside of this method. s.x = value; } private void HeapStackTests(bool Optimized, int RepeatCount) { long start, stop; int ix = 0; int result = 0; if (Optimized == false) { //No Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// start = PerformanceHelper.GetPerformanceCount(); for (; ix < RepeatCount; ix++) { ClassObject co = new ClassObject();

//////////

13

co.x = 1; PerformOperationOnClassObject(co, ix); //Class objects are reference types and methods always receive references to the original class object. Any change to the object inside of the method can be seen after the call. result += co.x; } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } else {

//////////

//Optimized Cursor.Current = Cursors.WaitCursor; //////////////////////////////////////////////// start = PerformanceHelper.GetPerformanceCount();

//////////

for (; ix < RepeatCount; ix++) { StructObject so = new StructObject(); so.x = 1; PerformOperationOnStructObject(so, ix); //Structure objects are value types and methods receive always a copy of the original structure. Any change to the object inside of the method cannot be seen after the call. result += so.x; //so.x is allways equal to 1 because the method call copies the value type and did not pass a reference to the original structure } stop = PerformanceHelper.GetPerformanceCount(); //////////////////////////////////////////////// ////////// Cursor.Current = Cursors.Default; DisplayResults(start, stop); } }

Now, uncomment the call to the HeapStacksTest method youve just pasted, in the menuHeapStack_Click event handler.

13. Now run the Stack vs. Heap 5 times, and write down the results here, and take a mean value.

14

# 1 2 3 4 5 Mean

Non Optimized

Optimized

14. If you want to run all the tests, you can open the final solution here.

15

You might also like