Introduce generics to Visual Basic programmers (2)

zhaozj2021-02-11  185

(Connected)

As you can see, generic use is very simple. Strong type code can avoid runtime errors; intelligent awareness works better. Although there is a very good reason to use generics, there are more advantages: performance and code reuse.

One main reason for introducing generic technology .NET framework is to improve performance. For example, the collection class can be faster than previously because the compiler can optimize the type stored in the collection. The following code compares arrays, ArrayList, and generic LIST performance:

TXTOUTPUT.TEXT = "Performance" & vbcrlf

Const Itemions as integer = 5000000

Perftime.Start ()

DIM MyArray (Items) AS Integer

For i as integer = 0 to itys - 1

MyArray (i) = i

NEXT

DIM ELAPSED AS INTEGER = Perftime.Stop

TXTOUTPUT.TEXT & = "Array Time:" & Elapsed & Vbcrlf

MyArray = Nothing

Gc.collect ()

Perftime.Start ()

DIM MyArraylist as new arraylist

For i as integer = 0 to itys - 1

MyArrayList.Add (i)

NEXT

Elapsed = Perftime.stop

TXTOUTPUT.TEXT & = "ArrayList Time:" & Elapsed & Vbcrlf

MyArrayList = Nothing

Gc.collect ()

Perftime.Start ()

DIM MYLIST AS New List (Of Integer)

For i as integer = 0 to itys - 1

MyList.Add (i)

NEXT

Elapsed = Perftime.stop

TXTOUTPUT.TEXT & = "List Time:" & Elapsed & Vbcrlf

Mylist = Nothing

Gc.collect ()

This code stores 5 million values ​​in an array of fixed lengths, and also stores the same more values ​​in automatic growth of ArrayList and generic LIST, and the performance value looks very interesting:

Array Time: 344

ArrayList Time: 4656

List time: 797

There is a specific type of fixed length array has unparalleled speed, and does not need to pay for the change in size. The size of the collection type is automatically increased, and if there is a fixed array 1/2 performance is quite good. Next, let's take a look at ArrayList, very unfortunate, only the performance of the fixed data 1/10. The problem is designed in ArrayList to store reference variables, Integer is a value type, and it is necessary to "pack" operation before storage to arraylist, and convert Integer to Object. The cost of packing is very expensive, so when you save value type data (such as Integer, Date, Boolean, and Structure, you can create Structure, you can get very significant performance).

For more information on "Packing" and "Unpacking" operation, see "Packing Conversion" and "Unpacking Conversion" in the MSDN library.

Create generic types and methods

Not only using the generic type provided by Visual Basic.Net, you can create your own generic types and methods. Generic method

When you want to implement some general algorithms related to a specific type, you may want to create a generic method. For example, a typical bubble sort requires all items in arrays, two two comparisons, and exchanges the values ​​that require sorting.

If you have already determined as long as you are sorting, you can simply write a SWAP method that can only be used for Integer types. But if you want to be able to sort any type, you can write a generic SWAP method as follows:

Private sub swap (of itemtype) _

(Byref v1 as itemtype, byref v2 as itemtype)

DIM TEMP As ItemType

Temp = V1

V1 = V2

V2 = TEMP

End Sub

Note "of itemtype" When the SWAP method is called, in addition to providing the required parameters, you must also pass a data type. This data type replaces ItemType in any instance. The following example calls SWAP:

Swap (of integer) (V1, V2)

This statement tells the SWAP method that it will exchange the Integer type. If you go back to see the Swap code, this statement means that JIT will replace all ItemType to Integer, this SWAP method is actually rewritten by JIT:

Private sub swap (byref v1 as integer, byref v2 as integer)

DIM TEMP AS INTEGER

Temp = V1

V1 = V2

V2 = TEMP

End Sub

This is the actually implemented code, JIT generates a method dedicated to an Integer type. If you want to sort a string type, you can use another SWAP as follows:

SWAP (OF STRING) (V1, V2)

When the method is executed, JIT generates another version of SWAP, this time is a specific String type:

Private sub swap (byref v1 as string, byref v2 as string)

DIM TEMP As String

Temp = V1

V1 = V2

V2 = TEMP

End Sub

Below is a complete example of a bubbling of generic swap:

Private sub btnsortintegers_click (Byval E AS System.EventArgs) Handles Btnsortintegers.click

DIM INTS (9) AS Integer

Dim r as new random

For i as integer = 0 to 9

INTS (i) = r.next (1, 100)

NEXT

' Bubble Sort

For J AS Integer = 0 to 9

Fork as integer = 9 to 1 step -1

IF INTS (K)

SWAP (INTEGER) (INTS (K), INTS (K - 1))

END IF

NEXT

NEXT

TXTOUTPUT.TEXT = "sort integers" & vbcrlf

For i as integer = 0 to 9

TXTOUTPUT.TEXT & = INTS (i) & vbcrlf

NEXT

End Sub

Generic type

Last point, you can create a fully generic type, using this "of itemtype" method, creating a classes as follows: Public Class SomeClass (of itemtype)

Private InternalVar as ItemType

Public Function SomeMethod (Byval Value As ItemType) AS ITEMTYPE

END FUNCTION

END CLASS

This code is the same for the role of the class. The JIT compiler will simply replace the itemType in the instance to be specified when instantiated.

constraint

Pan technology also supports a characteristic called constraint. This feature ensures that the type of incoming type at least to implement some functions at the time of the specified type. For example, you want to implement a sort algorithm, you need to ensure that the incoming type can implement the ICompArible interface. You can use constraints to complete this idea:

Public Class SomeClass (of itemtype as icomprole)

Public Function SomeMethod (Byval Value As ItemType) AS ITEMTYPE

END FUNCTION

END CLASS

in conclusion

Pan technology provides a lot of benefits relative to Object-based collection, first, generic classes are strong type, which ensures that all errors can be found when compiling. Strong types can make intelligent awareness more convenient. The generic can make you simplify the code, so that your algorithm can act in a variety of types. Finally, the generic collection is much more faster than the Object, especially when the value type is used.

转载请注明原文地址:https://www.9cbs.com/read-5119.html

New Post(0)