[RESOLVED] Fastest way to save/load variant array to/from file
A have a variant array that could be one or two dimensions, could hold any data type, and I want to be able to save it to a file and read it back in from that file as quickly as possible.
What's the absolute fastest way to do this? I don't much care what the actual file format ends up being. (Text, binary, whatever.)
Re: Fastest way to save/load variant array to/from file
Binary is the fastest way. In that way it states the same as it was in memory. So you would need to use arrays then write the whole array at once.
Re: Fastest way to save/load variant array to/from file
That sounds ideal. Could you start me off with a code snippet? Doesn't have to be anything fancy.
Re: Fastest way to save/load variant array to/from file
What kind of structure are you attempting to write to file? Post an example to work with.
Re: Fastest way to save/load variant array to/from file
In QBasic, I would recommend BSAVE/BLOAD, but I see those commands are not in VB. :(
Mac
Re: Fastest way to save/load variant array to/from file
Quote:
Originally Posted by randem
What kind of structure are you attempting to write to file? Post an example to work with.
The structure is variable. Here's some examples:
Code:
Sub Main()
Dim varArray As Variant
Dim lngArray() As Long
Dim strFile As String
Dim i As Long
strFile = App.Path & "\Array"
' Two-dimensional
ReDim varArray(1,1)
varArray(0,0) = 1
varArray(0,1) = "Hello"
varArray(1,0) = 2
varArray(1,1) = ", World!"
SaveArrayToFile varArray, strFile
Erase varArray
LoadArrayFromFile varArray, strFile
Debug.Print varArray(0,1) & varArray(1,1)
Erase varArray
' Single-dimension Date
ReDim varArray(10)
For i = 0 To 10
varArray(i) = DateAdd("d", i, Now())
Next
SaveArrayToFile varArray, strFile
Erase varArray
LoadArrayFromFile varArray, strFile
Debug.Print "3 days from now is " & varArray(3)
Erase varArray
' Typed single-dimension Long
ReDim lngArray(10)
For i = 0 To 10
lngArray(i) = 7 * i
Next
SaveArrayToFile lngArray, strFile
Erase lngArray
LoadArrayFromFile lngArray, strFile
Debug.Print "7 * 9 = " & lngArray(9)
Erase lngArray
' Single-dimension string
ReDim varArray(1)
varArray(0) = "Hello,"
varArray(1) = " World!"
SaveArrayToFile varArray, strFile
Erase varArray
LoadArrayFromFile varArray, strFile
Debug.Print varArray(0) & varArray(1)
Erase varArray
End Sub
Public Sub SaveArrayToFile(pvarArray As Variant, pstrFile As String)
Dim strFile As String
Dim strHeader As String
strFile = pstrFile & ".dat"
strHeader = pstrFile & ".hdr"
???
End Sub
Public Sub LoadArrayFromFile(pvarArray As Variant, pstrFile As String)
Dim strFile As String
Dim strHeader As String
strFile = pstrFile & ".dat"
strHeader = pstrFile & ".hdr"
???
End Sub
No need to worry about validating that the passed array is in fact an array; I can handle all the validation. Also, if saving the structure information to the same file is just as fast, no need to split it off to a second file.
So the arrays will hold any native data type (no UDTs or objects) and be either 1- or 2-dimensional. The passed arrays may be originally declared either as a typed array or simply as a Variant.
Re: Fastest way to save/load variant array to/from file
This function may be of help:
Code:
' Returns 0 for unintialized array, -1 for non-array
Public Function ArrayDimensions(pvarArray As Variant) As Long
Dim lngTemp As Long
Dim i As Long
On Error Resume Next
Do
i = i + 1
lngTemp = UBound(pvarArray, i)
Select Case Err.Number
Case 13: ArrayDimensions = -1
Case 9: ArrayDimensions = i - 1
End Select
Loop Until Err.Number
End Function
Re: Fastest way to save/load variant array to/from file
I'm hurtin' for a solution on this one. Does nobody have any ideas?
Even if it's not all-purpose, I would appreciate any kind of approach. How about we start off with just a single-dimension variant array holding Longs.
Anyone?
Re: Fastest way to save/load variant array to/from file
Could you repost your code in post #6 or replace those vbcode tags with just the code tags. Those numbers are annoying to get rid of in a copy/paste. I will implement something.
Re: Fastest way to save/load variant array to/from file
No problem, sorry about that.
1 Attachment(s)
Re: Fastest way to save/load variant array to/from file
Re: Fastest way to save/load variant array to/from file
Aw yeah, now that is what I'm talking about.
Thanks much, randem. It's as simple as simple gets. You rock.