Results 1 to 4 of 4

Thread: Which is the best method

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9

    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?

  2. #2
    New Member
    Join Date
    Mar 2005
    Posts
    7

    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:
    1. Private Sub TheProcess()
    2.         Dim StartTime As DateTime
    3.         Dim DoneTime As DateTime
    4.         Dim Reps As Integer = 500
    5.         Dim x As Integer
    6.         StartTime = Now
    7.  
    8.         For x = 0 To Reps
    9.  
    10.             'Your Code here
    11.  
    12.         Next
    13.         DoneTime = Now
    14.         MsgBox(DateDiff(DateInterval.Second, StartTime, DoneTime).ToString)
    15.     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

  3. #3
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  4. #4

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    9

    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
  •  



Click Here to Expand Forum to Full Width