Search Tools Links Login

Swap Variables 12 times Faster!


Visual Basic 6, or VB Classic

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

Inputs

2 variables

Returns

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)

Code

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
' (http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=1&txtCodeId=8426)
' 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: 144 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.