VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Swap Variables 12 times Faster

by Bradley Liang (6 Submissions)
Category: String Manipulation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (4 Votes)

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

Inputs
2 variables
Code 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)

Rate Swap Variables 12 times Faster

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

Download this snippet    Add to My Saved Code

Swap Variables 12 times Faster Comments

No comments have been posted about Swap Variables 12 times Faster. Why not be the first to post a comment about Swap Variables 12 times Faster.

Post your comment

Subject:
Message:
0/1000 characters