bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Q: what is a fast way to see if an 'item' is present in an array?


From: Linda Walsh
Subject: Q: what is a fast way to see if an 'item' is present in an array?
Date: Mon, 15 Feb 2016 19:59:21 -0800
User-agent: Thunderbird

I has a little 4 line func that returns true or false
if a string was in an array

I came up with a different way of doing it, that I
though might be faster... but...
putting them in a file and running loops on them.
(times below).  Obviously, my feeling for what might
be faster was way wrong. w/my 2nd method
being 15-44X slower depending on where the match is.

Obviously, If I could precook the array into
a hash, I'd think it would be faster... but
the "cooking part", I think, is the high
overhead part.

The fast function was:
my fn1='() {
   my __
   my t="${2:address@hidden"
   for __ in "${!t}"; do [[ $1 == "$__" ]] && return 0;done
   return 1;
}
'
w/the slow func  being killed by a $() sub process, likely:
my fn2='() { my t="${2:?}[*]"
   my arRE="^($(IFS="|"; echo "${!t}"))$"
   [[ $1 =~ $arRE ]]
}
'

Was wondering if anyone else had fast functions like this
to determine if a string is in a list?

I can attach the test script (doubles as an "include" or
"source" file), if people want to test their own...
The frameworks should be simple to copy in other test
functions...(or replace #2...)...

This was in bash-4.3.42.

---data results---



(-t [#loops] string WORDLIST):

so false case 1st:
_in.shh -t 300 word {0..999}
func(fn1): 1.49sec 1.48usr 0.00sys (100.02% cpu)
false
func(fn2): 22.78sec 18.38usr 4.32sys (99.69% cpu)
false

then true w/match @ end of the list

_in.shh -t 300 word {0..999} word
func(fn1): 1.49sec 1.49usr 0.00sys (100.03% cpu)
true
func(fn2): 22.85sec 18.46usr 4.31sys (99.67% cpu)
true

then true w/match @ beginning of list...

 _in.shh -t 300 word word {0..999}
func(fn1): 0.51sec 0.51usr 0.00sys (100.01% cpu)
true
func(fn2): 22.75sec 18.40usr 4.28sys (99.70% cpu)
true





reply via email to

[Prev in Thread] Current Thread [Next in Thread]