Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / containers / virtual-machine

When Memory Writes Are Slower Than Disk Writes

1.16/5 (7 votes)
17 Oct 20072 min read 1   156  
When is writing to memory slower than writing to disk? When it's VB 6
Slow Memory Main Screen

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:

VB.NET
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 BStrings) 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
    1. Opens a file, writes the begin time, closes file
    2. Concatenates to the string 1,000,0000 times
    3. Opens file, writes end time, closes file
    4. Watch out, it takes 18 or 19 minutes on modern hardware
  • Concat File button
    1. Opens a file #1, writes the begin time, closes file
    2. Opens file #2 (concat file)
    3. Concatenates to the file (#2) 1,000,0000 times
    4. Closes file #2
    5. Opens file #1, writes end time, closes file #1
    6. Takes only 1 - 2 seconds

Here's the entire listing of code, because it is so short:

VB.NET
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).

  1. c:\VBStringTime.txt -- contains start and stop times for process
  2. c:\VBFileTime.txt -- contains start and stop times for process
  3. 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here