Introduction
Would you ever believe that writing (concatenating to memory) is slower (by 100 times) than writing to disk? Well, this VB 6 program proves that under the VB6 Virtual Machine it was a ton slower.
Background
While attempting to determine why a VB6 program which was given to me was so terribly slow, I stumbled across a line of code that looked like the following:
strFileData = strFileData & strMoreFiledata
All that line is doing is concatenating to a VB String (BString
) so it shouldn't be slow. However, when you learn more about the way VB strings are stored (as BString
s) you find that the max size for a BString
is 65535 (2 byte integer).
So, whenever the string becomes larger than that, the system has to do a malloc each time something is concatenated to the string.
Using the Code
Get the source and add open with the Visual Basic 6 or just run the provided executables.
It's just two buttons:
- Concat String button
- Opens a file, writes the begin time, closes file
- Concatenates to the string 1,000,0000 times
- Opens file, writes end time, closes file
- Watch out, it takes 18 or 19 minutes on modern hardware
- Concat File button
- Opens a file #1, writes the begin time, closes file
- Opens file #2 (concat file)
- Concatenates to the file (#2) 1,000,0000 times
- Closes file #2
- Opens file #1, writes end time, closes file #1
- Takes only 1 - 2 seconds
Here's the entire listing of code, because it is so short:
Private Sub cmdConcatString_Click()
Dim str As String
Open "c:\VBStringTime.txt" For Append As #1
Write #1, "####### START #######"
Write #1, Now()
Close #1
For x = 1 To 1000000
str = str & "A"
Next x
Open "c:\VBStringTime.txt" For Append As #1
Write #1, "####### END #######"
Write #1, Now()
Close #1
End Sub
Private Sub cmdConcatToFile_Click()
Dim str As String
Open "c:\VBFileTime.txt" For Append As #1
Write #1, "###### START #####"
Write #1, Now()
Close #1
str = "A"
Open "c:\VBFileConcat.txt" For Append As #2
For x = 1 To 1000000
Write #2, str
Next x
Close #2
Open "c:\VBFileTime.txt" For Append As #1
Write #1, "###### End #####"
Write #1, Now()
Close #1
End Sub
After Running
After running, you'll have a total of three files (if you clicked each button).
- c:\VBStringTime.txt -- contains start and stop times for process
- c:\VBFileTime.txt -- contains start and stop times for process
- c:\VBFileConcat.txt -- contains strings written to file A
How Long It Takes
String Concatenation (memory)
This is what the VBStringTime.txt shows:
"####### START #######"
#2007-10-03 13:59:25#
"####### END #######"
#2007-10-03 14:18:13#
That's right. It took almost 19 minutes to do 1,000,000 memory concatenations!
File Concatenation (disk)
"###### START #####"
#2007-10-03 14:14:39#
"###### End #####"
#2007-10-03 14:14:40#
That's right. Somewhere in the range of 1 to 2 seconds! What???
Conclusion
Things are not always what they seem. Take the code and run it and you'll be amazed.