Click here to Skip to main content
15,867,308 members
Articles / Containers / Virtual Machine
Article

When Memory Writes Are Slower Than Disk Writes

Rate me:
Please Sign up or sign in to vote.
1.16/5 (7 votes)
17 Oct 20072 min read 26.9K   154   12   9
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


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
prakashr198420-Apr-10 19:58
prakashr198420-Apr-10 19:58 
GeneralThings are indeed not what they appear : ) Pin
agile_codeslinger30-Jan-09 2:50
agile_codeslinger30-Jan-09 2:50 
QuestionWhat about MFC / C++? Pin
daylightdj20-Nov-07 3:55
daylightdj20-Nov-07 3:55 
So what about MFC and C++? How well does it handle string concat?

Here's MFC (using CString class) / C++ example code. You can create a quick application and add this method and try it for yourself.

Using MFC it takes less than a 2 seconds to do the concat!

<br />
void CConcatCDlg::OnBnClickedConcat()<br />
{<br />
	CString csA;<br />
<br />
	CFile cfOut("c:\\loopCpp.txt",  CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);<br />
	<br />
	CTime myTime = CTime::GetCurrentTime();<br />
	<br />
	CString outTime = myTime.Format("%H:%M:%S");<br />
		<br />
	cfOut.Write(outTime,outTime.GetLength());<br />
	cfOut.Write("\n",1);<br />
	for (int x = 0; x < 1000000;++x)<br />
	{<br />
		csA += "A";<br />
	}<br />
	myTime = CTime::GetCurrentTime();<br />
//	CFile cf2("c:\\loopCpp.txt",  CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);<br />
	//cf2.Open("c:\\loopCpp.txt",  CFile::modeCreate |  CFile::modeNoTruncate | CFile::modeWrite);<br />
	outTime = myTime.Format("%H:%M:%S");<br />
	cfOut.Write(outTime,outTime.GetLength());<br />
	cfOut.Write("\n",1);<br />
//  UNCOMMENT FOLLOWING LINE IF YOU WANT TO BE CONVINCED the csA really has 1,000,000 chars in it<br />
//	cfOut.Write(csA,csA.GetLength());<br />
	cfOut.Close();<br />
<br />
}<br />


Isn't it interesting that the original API wrappers (MFC) were so efficient?
I know there wasn't any garbage collection and all that, but it is just interesting.
GeneralSemi-efficient VB6 approach Pin
supercat916-Nov-07 6:25
supercat916-Nov-07 6:25 
GeneralRe: Semi-efficient VB6 approach Pin
daylightdj20-Nov-07 2:07
daylightdj20-Nov-07 2:07 
GeneralStringBuilder Pin
nxtwothou18-Oct-07 3:13
nxtwothou18-Oct-07 3:13 
GeneralRe: StringBuilder Pin
daylightdj18-Oct-07 10:12
daylightdj18-Oct-07 10:12 
GeneralRe: StringBuilder Pin
nxtwothou18-Oct-07 10:40
nxtwothou18-Oct-07 10:40 
GeneralRe: StringBuilder Pin
Tayab15-Feb-08 15:19
Tayab15-Feb-08 15:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.