|
|
Title | Copy memory quickly using MemCopy (RtlMoveMemory) in VB .NET |
Description | This example shows how to copy memory quickly using MemCopy (RtlMoveMemory) in VB .NET. |
Keywords | RtlMoveMemory, copy memory, MemCopy, CopyMemory, array, memory |
Categories | Tips and Tricks, Miscellany, Software Engineering, VB.NET |
|
|
When you click the Build Array button, the program makes two arrays containing Longs of a specified size. When you click the For Loop button, the program copies the values from one array to the other using a For loop.
When you click Assignment, the program sets Array2 = Array1. This doesn't work for copying only part of the array but it is fast and easy.
When you click MemCopy, the program uses the RtlMoveMemory API function.
|
|
' Declare the memory copying funciton.
Private Declare Sub CopyMemory Lib "kernel32" Alias _
"RtlMoveMemory" (ByVal Destination As Long, ByVal _
Source As Long, ByVal Length As Integer)
Private Array1() As Integer
Private Array2() As Integer
Private NumItems As Integer
Private Bytes As Integer
Private Sub btnBuildArray_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnBuildArray.Click
Me.Cursor = Cursors.WaitCursor
lblTime.Text = ""
Application.DoEvents()
Bytes = CInt(SizeText.Text) * 1024 * 1024
NumItems = Bytes \ Len(NumItems)
ReDim Array1(NumItems - 1)
ReDim Array2(NumItems - 1)
btnForLoop.Enabled = True
btnAssignment.Enabled = True
btnMemCopy.Enabled = True
Me.Cursor = Cursors.Default
End Sub
' Copy using a For loop.
Private Sub btnForLoop_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnForLoop.Click
Dim start_time As Date
Dim stop_time As Date
Dim elapsed_time As TimeSpan
Me.Cursor = Cursors.WaitCursor
lblTime.Text = ""
Application.DoEvents()
start_time = Now
For i As Integer = 0 To NumItems - 1
Array2(i) = Array1(i)
Next i
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblTime.Text = _
elapsed_time.TotalSeconds.ToString("0.0000")
Me.Cursor = Cursors.Default
End Sub
' Assign one array to the other.
Private Sub btnAssignment_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles _
btnAssignment.Click
Dim start_time As Date
Dim stop_time As Date
Dim elapsed_time As TimeSpan
Me.Cursor = Cursors.WaitCursor
lblTime.Text = ""
Application.DoEvents()
start_time = Now
Array2 = Array1
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblTime.Text = _
elapsed_time.TotalSeconds.ToString("0.0000")
Me.Cursor = Cursors.Default
End Sub
' Copy using MemCopy.
Private Sub btnMemCopy_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnMemCopy.Click
Dim start_time As Date
Dim stop_time As Date
Dim elapsed_time As TimeSpan
Me.Cursor = Cursors.WaitCursor
lblTime.Text = ""
Application.DoEvents()
start_time = Now
CopyMemory(Array2(0), Array1(0), Bytes)
stop_time = Now
elapsed_time = stop_time.Subtract(start_time)
lblTime.Text = _
elapsed_time.TotalSeconds.ToString("0.0000")
Me.Cursor = Cursors.Default
End Sub
|
|
In my tests, assignment and MemCopy were much faster than copying with a For loop. In fact, they were so fast that I didn't see any difference until the array got so big that paging and other system issues interfered with the results.
Because assignment is roughly as fast as RtlMoveMemory and is much easier to understand, you should use it to copy whole arrays. If you need to copy only part of an array, you can use RtlMoveMemory to avoid using a For loop.
|
|
|
|
|
|