Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to put lines with certain text (from a file) in an array

How about run the test yourself and you will see?
Just add some extra StartSW and StopSW and it will all be revealed.

RBS


"Rick Rothstein" wrote in message
...
My question was referring to physical elapsed time per loop, not relative
percentage speed. The reason I asked that question is if the entire
process (read, process, save) takes, say, 5 seconds to complete and the
part of the code in question takes either an 1/8 second for the fast code
or 1/4 second for the slow code, I would not think that a significant time
difference, even though one is half as fast as the other, when compared to
the entire process the code is part of. In other words, reading a file and
then saving a file will more than likely take up the bulk of the time and
that is what the user will notice, not the relative time difference for a
portion of the entire process.

--
Rick (MVP - Excel)


"Peter T" <peter_t@discussions wrote in message
...
I didn't save the original test. I've made a new test with somewhat
different data and seem to be getting a very different set of results this
time. In one sense all consistent but now the Filter approach is taking
about 2x longer than the loop with Instr with all sizes. (Previously 10Mb
was only about 25% slower with the Filter method, but 1Mb an odd 3x
slower).

I'm pretty sure I had double checked my results last time. Maybe somehow
I got it wrong or as I suspect, in the past I've also had inconsistent
results with large strings, who knows. Here's what I tested this time -

Option Explicit
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Const cFILE As String = "c:\temp\TestFile#.txt"

Sub MakeTestFiles()
Dim i As Long
Dim sFile As String, sText As String
Dim a(1 To 5) As String
Dim ff As Integer

a(1) = "This is layer_1"
a(2) = "this line does not have any layers"
a(3) = "Embedded at the end of this line is Layer_3"
a(4) = "A layer_4 in this fourth line"
a(5) = "This will be the last line with layer_5"

sText = Join(a, vbCrLf)
Do
sText = sText & vbCrLf & sText
If Len(sText) 20000 Then
i = i + 1
sFile = Replace(cFILE, "#", i)
ff = FreeFile
Open sFile For Output As #ff
Print #ff, sText
Close #ff
Debug.Print i, Len(sText), sFile
End If

Loop Until Len(sText) 10000000
' 10 files from 22Kb to 11Mb

End Sub

Sub CompareFilterLoop()
Dim ff As Integer
Dim i As Long, k As Long, n As Long, nSize As Long
Dim tFilter As Long, tLoop As Long
Dim sFile As String, sText As String
Dim arr1, arr2

For k = 1 To 10

ff = FreeFile
sFile = Replace(cFILE, "#", k)
Open sFile For Binary As #ff
nSize = LOF(ff)
sText = Space(nSize)
Get #ff, , sText
Close #ff

arr1 = Split(sText, vbCrLf)
If IsArray(arr2) Then Erase arr2

tFilter = GetTickCount
arr2 = Filter(Filter(arr1, "layer_", True, vbTextCompare), _
"layer_3", False, vbTextCompare)
tFilter = GetTickCount - tFilter

Erase arr2

tLoop = GetTickCount
ReDim arr2(0 To UBound(arr1)) As String
n = 0
For i = 0 To UBound(arr1)
If InStr(1, arr1(i), "layer_", vbBinaryCompare) 0 And _
InStr(1, arr1(i), "layer_3", vbBinaryCompare) = 0 Then
arr2(n) = arr1(i)
n = n + 1
End If
Next i
ReDim Preserve arr2(0 To n - 1) As String

tLoop = GetTickCount - tLoop
Debug.Print tFilter, tLoop, nSize, UBound(arr1), UBound(arr2)

Next

End Sub

For me the filter method was roughly 2x slower with all sizes above 300k
where timings are meaningful

Regards,
Peter T



"Rick Rothstein" wrote in message
...
Out of curiosity, how much faster was "much faster" for a single loop if
your test involved multiple loops (total time divided by number of
loops)?

--
Rick (MVP - Excel)





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
number of lines and characters in a text file Bob Flanagan[_2_] Excel Programming 2 August 2nd 08 05:09 PM
Copying certain lines from a text file Michael A Excel Discussion (Misc queries) 3 February 13th 07 08:08 PM
Deleting text file lines RomanR Excel Programming 3 July 28th 06 03:43 PM
excel97 vba to append lines to text file overwriting last 2 lines Paul Excel Programming 1 November 6th 04 08:11 PM
FileSystemObject to get last 10 lines of text file. john Excel Programming 11 July 16th 03 03:54 PM


All times are GMT +1. The time now is 07:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"