Re: stupid question



I don't think the point is to be able to "boast
of the best method". Personally I find optimizing very
interesting and useful. There's not a heck of a lot of
optimizing that can be done in script, but as a matter
of valuing good design and general mindfulness in one's
endeavors, I like to try to optimize wherever I can.

There's a very interesting site for VB optimizing that
compares different methods:
http://www.xbeat.net/vbspeed/
The differences can often be very significant in "real
world" usage. Unfortunately, though, while VB is a
somewhat inefficient tool that can be optimized to very
high efficiency, VBScript is a very inefficient tool that
can only be minimally optimized.


In the case at hand I doubt that, in most scenarios,
there will be a significant difference. On the other hand,
I didn't know exactly what the OP was doing...so why
not optimize? Why not try to understand how the code
works and be articulate about it?

I don't happen to have a list of 23,000 files handy, so
I don't know what the "insignificant" difference is that you
found. A couple points that might be relevant:

* A tiny difference in checking on one file could translate
into a notable difference if thousands of files need to be
checked against the list.

* I don't know how accurate the time method is for measuring
tiny durations. It may not even be accurate to 100 ms. I really
have no idea. But given that a single search of an array or string
is involved, extreme accuracy down to 1 ms would be needed
to assess real world implications.

It may be that there's virtually no difference, even where
tens of thousands of files are checked for tens of thousands
of names. Instr, array accessing, and string comparison are
all very fast. That result would still be interesting to know, as
far as I'm concerned. Isn't it more interesting to know the facts
than to know who gets "boasting rights"?


OTOH: His delimited approach seems a bit lackluster. (no disrespect to
mayayana)

That's a good point. I guess it would need a delimiter
on both ends.

No comment as to which method is faster though.<shrug>

I imagine that the array check is plenty fast for most uses,
but the OP referred to a "huge" number of folder names to
check, so I thought it might be worth his while to optimize
the operation. I figure that the bigger the number, the better
the string method. My reasoning is that once the delimited string
has been created, all folder names can be filtered with one
case-insensitive InStr, which is nearly instant no matter how
big the string gets. With the array method, on the other hand,
each folder name requires a separate binary comparison and
probably two calls to UCase.

Ok, you forced me to comment...

Based on your "once the delimited string has been created" comment, my
testing of InStr to the Ucase(array) method after variable/array is in
memory, the comparison seems to evaluate to an insignificant time
difference
on a list of 23000 fully qualified file names. Testing was done with
nonexistent strings as well as lines found in various positions in the
list.

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("list.txt", 1)
MyArray = Split(f.ReadAll, vbCRLF)
s = "*" & Join(MyArray, "*") & "*"
f.Close
Wscript.Echo "Array and variable are now in memory..."

name = "some text to find in list"

'Array Method
begin = time
found = false 'initialize flag
For i = Lbound(MyArray) To Ubound(MyArray)
If Ucase(name) = Ucase(MyArray(i)) Then
found = true 'item was found so toggle flag
Exit For 'and abort the search
End If
Next
Wscript.Echo "Array Method", time - begin

'InStr Method
begin = time
If Instr(1, s, "*" & name & "*", 1) > 0 then
'-- there's a match
End if
Wscript.Echo "Instr Method", time - begin


Based on my testing results, how "huge" does the list need to be for one
to
explore code optimization? Likewise, how significant does the difference
need to be for one to boast one method over another?

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)



.



Relevant Pages

  • Re: Byte array as string?
    ... > least comparable - to loop through an array ... > than to use Instr. ... on a string. ... "beep" and the time taken for the code ...
    (microsoft.public.vb.general.discussion)
  • Re: Number of occurances
    ... twice as long then the one based on the byte array will already beat the one based on Instr. ... The advantage of the one with Instr is though that the string to find can be more than ...
    (microsoft.public.vb.general.discussion)
  • Re: Open File For Input Method Faster Than File System Object Stream?
    ... Much depends on exactly what you are doing, and the best method to advise would depend chiefly on whether you are interested in only the first occurrence of the substring in the file or whether you want to check every line of the file for that substring. ... Dim s1 As String, fn As Long ... You could then use Instr in a loop to run through the entire string just once, searching for and recording (in an array of Longs) the position of every linefeed character it finds. ...
    (microsoft.public.vb.general.discussion)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)