|
-
Jun 16th, 2005, 11:17 AM
#1
Thread Starter
New Member
Which is the best method
I am working on a program that reads characters from a data file (either 0, 1, or 2) and draws the corresponding colored rectangle using GDI+. The way it is set up is I have a loop that reads one character (using StreamReader.read() )and draws the rectangle, it repeats the process until it reaches the end of the text file.
The program works fine, but my question is, is this the best and fastest way to do it, or would it be better to write the file or characters to an array and then draw from the array?
-
Jun 16th, 2005, 12:21 PM
#2
New Member
Re: Which is the best method
Theoretically, it would be faster to read it into an array first, because the whole set of integers would be spun up into in-process memory and wouldn't haven't to read from the HardDrive each line. However, most likely your text file is so small that once it reads the first line, Windows will hold the rest in your OS memory anyway so it really wouldn't make a difference. Why don't you test it?
Automate the thing so that the whole process is wrapped in a For Next loop with time stamps like this.
VB Code:
Private Sub TheProcess()
Dim StartTime As DateTime
Dim DoneTime As DateTime
Dim Reps As Integer = 500
Dim x As Integer
StartTime = Now
For x = 0 To Reps
'Your Code here
Next
DoneTime = Now
MsgBox(DateDiff(DateInterval.Second, StartTime, DoneTime).ToString)
End Sub
The MessageBox at the end would show how many seconds to complete 500 (or however many) repetitions.
You could build one of these procedures for both methods and see if one takes longer.
Last edited by KentDMc; Jun 16th, 2005 at 12:21 PM.
Reason: typo
-
Jun 16th, 2005, 02:30 PM
#3
Re: Which is the best method
I'm not sure exactly how you have structures your file but I, personally, would read everything in at once (then close the file), then interpret the data from memory and draw the rects as needed. That would be much faster becasuse the (slow) read operation doesn't have to yield to the (faster) drawing ops.
It would probably be worthwhile re-thinking your file format too (unless you are stuck with what you've got).
I don't live here any more.
-
Jun 20th, 2005, 02:54 PM
#4
Thread Starter
New Member
Re: Which is the best method
Thanks for the help. That's an excellent idea Kent, I used the code you posted and found that reading everything and then drawing it was significantly faster, as you guys had said. Thanks again!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|