Designing Types

I l @ ve RuBoard

You might recall from Chapter 2 the distinction between value types and reference types. I said that value types demonstrate intrinsically better performance in accessing members than reference types do because value types are stack-based entities and reference types live in the heap.

Let's examine this further. I created a sample to illustrate this situation. I created two types, PointStruct and PointClass , that both serve to contain a pair of x and y coordinates. You can see from the following definition that they're exactly equivalent:

 StructurePointStruct Publicx,yAsDouble PublicSubNew(ByValxCoordAsDouble,ByValyCoordAsDouble) x=xCoord y=yCoord EndSub EndStructure ClassPointClass Publicx,yAsDouble PublicSubNew(ByValxCoordAsDouble,ByValyCoordAsDouble) x=xCoord y=yCoord EndSub EndClass 

What we'll do with these is simple. We'll create a large array of both types and insert random data. Then we'll iterate through the arrays of points, calculating the slope and distance between adjacent points and at the same time calculating the length of the overall line represented by the respective array (which we won't do anything with).

The ValueTypePerformance example (one of this chapter's samples files) demonstrates what the performance looks like for more intensive calculations ”which is, after all, where the memory advantages of the value type become apparent. So even though the following might cause flashbacks to 10th-grade math class, it's a worthwhile example:

 ModuleModule1 ConstARRAY_SIZEAsInteger=10000 SubStructTest() DimiAsInteger Dimx,yAsDouble DimrandAsNewRandom() 'InitializetheArray Dimps()AsPointStruct=NewPointStruct(ARRAY_SIZE){} Fori=0ToARRAY_SIZE x=rand.Next() y=rand.Next() ps(i)=NewPointStruct(x,y) Next 'Calculatetheslopeanddistancebetweeneachpoint DimlengthAsDouble=0 Dimslope,distanceAsDouble Dimdx,dyAsDouble Fori=1ToARRAY_SIZE dx=(ps(i).x-ps(i-1).x) dy=(ps(i).y-ps(i-1).y) slope=dy/dx distance=Math.Sqrt(Math.Pow(dx,2)+Math.Pow(dy,2)) length+=distance Next EndSub SubClassTest() DimiAsInteger Dimx,yAsDouble DimrandAsNewRandom() Dimpc()AsPointClass=NewPointClass(ARRAY_SIZE){} Fori=0ToARRAY_SIZE x=rand.Next() y=rand.Next() pc(i)=NewPointClass(x,y) Next 'Calculatetheslopeanddistancebetweeneachpoint DimlengthAsDouble=0 Dimslope,distanceAsDouble Dimdx,dyAsDouble Fori=1ToARRAY_SIZE dx=(pc(i).x-pc(i-1).x) dy=(pc(i).y-pc(i-1).y) slope=dy/dx distance=Math.Sqrt(Math.Pow(dx,2)+Math.Pow(dy,2)) length+=distance Next EndSub SubMain() DimiAsInteger DimsTime,cTimeAsTimeSpan Dimstart,finishAsDate start=Now() Fori=0To100 StructTest() Next finish=Now() sTime=finish.Subtract(start) start=Now() Fori=0To100 ClassTest() Next finish=Now() cTime=finish.Subtract(start) Console.WriteLine("StructTest{0,10}ticks",sTime.Ticks) Console.WriteLine("ClassTest{0,10}ticks",cTime.Ticks) Console.WriteLine(" {0,10:0.00%}",_ sTime.Ticks/cTime.Ticks) Console.ReadLine() EndSub EndModule 

From the following program output, you can see that the StructTest offers a performance advantage in this case. The performance difference is related solely to the additional overhead of accessing heap resident objects. Granted, the difference isn't earth-shattering, but a roughly 10 percent reduction in run ­time is generally considered a successful attempt at performance optimization.

 StructTest17781132ticks ClassTest19978800ticks 89.00% 

Does this mean that you should always use structures and avoid classes whenever possible? Absolutely not. Each type has its own advantages. In this case, structures were the obvious choice. But when the performance advantages aren't as significant or are even negligible (which is most of the time), classes are usually the best route. Ultimately, you have to determine what makes the most sense for your application's design. If you need to create a type that contains only data and little, if any, logic, a structure might be the better approach. Bottom line: use your own discretion.

Caution

You can quickly kill the performance advantage of a structure and end up worse off if you do a lot of boxing and unboxing. (See the section titled "Type Magic: Boxing and Unboxing" in Chapter 2.)


I l @ ve RuBoard


Designing Enterprise Applications with Microsoft Visual Basic .NET
Designing Enterprise Applications with Microsoft Visual Basic .NET (Pro-Developer)
ISBN: 073561721X
EAN: 2147483647
Year: 2002
Pages: 103

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net