Search Tools Links Login

Swap Variables 12 times Faster!

Swap Variable1 for Variable2 using API, this is usefull for creating data processing programs with many stored variables. See Info below

Original Author: Bradley Liang


2 variables


2 variables swapped

Side Effects

Eats 4 bytes of memory <-- no biggie

API Declarations

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Dest As Any, Source As Any, ByVal lNumBytes As Long)


Public Sub SwapStr(Var1 As String, Var2 As String)
' This is particularly useful in programs with lots of
' data analysis. Easily edited for any variant data
' manipulating. I'm currently using this coding and
' some vector codes to update my ThreeD Render Engine
' (
' a little advertising on my part =)...
' Using this routine is faster than
  ' sTmp = Var1
  ' Var1 = Var2
  ' Var2 = sTmp
' By a factor up 12 for really long values !!
Dim lSaveAddr As Long
' Save memory descriptor location for Var1
lSaveAddr = StrPtr(Var1)
' Copy memory descriptor of Var2 to Var1
CopyMemory ByVal VarPtr(Var1), ByVal VarPtr(Var2), 4
' Copy memory descriptor of saved Var1 to Var2
CopyMemory ByVal VarPtr(Var2), lSaveAddr, 4
'4 bytes is the size of one string. You may need to
'edit this coding a little in order to create memory
'efficient storage for different data types (i.e.
'user defined types).
End Sub

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 118 times


Visual Basic 6


No attachments for this post

Loading Comments ...


No comments have been added for this post.

You must be logged in to make a comment.