0

my program does work but i think it suffers performance issues, it is consuming upto 40% of my total CPU usage while it is looping the array. normally my programs would only consume under 5% of CPU usage, i think ReDim Preserve is causing this as i am looping around 100,000+ of lines. here is my code.

    Dim sArray As String()
    Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
    Dim sReader As New System.IO.StreamReader(fStream)
    Dim Index As Integer = 0
    Do While sReader.Peek >= 0
        ReDim Preserve sArray(Index)
        sArray(Index) = sReader.ReadLine
        Index += 1
    Loop

    fStream.Close()
    sReader.Close()

Is there any alternative way of placing values into an array aside from ReDim Preserve? Thanks in advance, i am really trapped into this problem right now.

Here is now my updated code using List.

    Dim sArray As String()
    Dim sList As New List(Of String)
    Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
    Dim sReader As New System.IO.StreamReader(fStream)
    Dim Index As Integer = 0
    Do While sReader.Peek >= 0
        sList.Add(sReader.ReadLine)
    Loop
    sArray = sList.ToArray

    fStream.Close()
    sReader.Close()

I still needed the funcionalities of an array so i created an array and placed the contents of the List into it.

Marc Intes
  • 737
  • 9
  • 25
  • 51
  • In one of the answers it has been suggested that you use a List, which I agree with. If for some reason you can't do that let us know. – dbasnett Jul 25 '13 at 14:19
  • i am having problems on where to start in converting it using List. a starting code could help – Marc Intes Jul 25 '13 at 14:23
  • Is there a reason not to just use [`File.ReadAllLines`](http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx)? – Brian Jul 26 '13 at 13:02

3 Answers3

6

You should use a List(Of String), which will leave room for future elements instead of resizing every time.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
3

As SLaks said best should be a List:

Dim sArray As New List(Of String)
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Do While sReader.Peek >= 0
    sArray.add(sReader.ReadLine)
Loop

fStream.Close()
sReader.Close()
David Sdot
  • 2,343
  • 1
  • 20
  • 26
  • thanks alot man this helped alot. i will post edit my code above with an updated one. – Marc Intes Jul 25 '13 at 14:42
  • What funcionalities of an array do you need? btw. you should accept SLaks answer, too ;) – David Sdot Jul 25 '13 at 14:53
  • Everything is done now, i am finally complete. it really boosted the performance, my program can now read 100,000+ lines in just a second. thanks a lot. – Marc Intes Jul 25 '13 at 14:54
  • If you are reading many lines might try replacing the end of stream test as well. Here is an alterative http://msdn.microsoft.com/en-us/library/db5x7c0d(v=vs.80).aspx You might squeeze a few more milliseconds out of the process. – rheitzman Jul 25 '13 at 15:35
1

Is seems like you are reading a file, an other option would be to use the ReadAllLines method.

Dim sArray() As String = System.IO.File.ReadAllLines("messages.txt")
the_lotus
  • 12,668
  • 3
  • 36
  • 53