in reply to Re: check whether a array element is equal to a string.
in thread check whether a array element is equal to a string.
Looks cool, but won't your solution require grep to loop through @new2 twice (once for each grep)?
Performance wise, isn't a single foreach loop, with two conditionals better?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: check whether a array element is equal to a string.
by JavaFan (Canon) on Jan 10, 2011 at 15:34 UTC | |
If you really care, benchmark. Both will do the same amount of comparisons, but the grep probably uses less opcodes. On top of that, the grep only does one assignment/push per array. Regardless of the number of matches. Not that I care. I find the grep version clearer: the important part is the modification (setting or pushing) of the arrays, and they are in front with the grep solution, instead of buried inside the foreach. And in general, I'd prefer a solution that doesn't need indentation over one that goes two levels deep. (Deepness of indentation is a rough estimate of complexity (yeah, yeah, hold the counterexamples)). | [reply] |
by raybies (Chaplain) on Jan 10, 2011 at 16:28 UTC | |
You mean, it's faster to do N * 2 comparisons than 2 * N comparisons? true. same number of comparisons. I suppose I was thinking in terms of fitting the comparison elements into the processor's register space. sorry, old asm programmer here. :) I agree it's a pretty cool solution. Thanks. | [reply] |
by ritchie (Initiate) on Jan 11, 2011 at 14:32 UTC | |
| [reply] [d/l] |