in reply to Re^2: Better way to do this
in thread Better way to do this

I need a faster way

Well. The easiest way to make code run faster, is to avoid doing things that don't need need to be done.

Reverseing an element array doesn't take very long, but since it makes no difference to the program, why bother.

I think that there are two or three things that could be changed to speed things up, one by a substantial amount; if I've correctly guessed the purpose of your code.

But since you seem reluctant explain the logic of your code, I've no way to assess the possibilities one way or another.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^4: Better way to do this
by PetreAdi (Sexton) on Feb 20, 2016 at 22:52 UTC

    Given two arrays @unique and @in, find the index, @idxs into @unique of the element of @in

    I want to return each elements which between two consecutive indexes (@idxs) if 10< Length > 5.

      Given two arrays @unique and @in, find the index, @idxs into @unique of the element of @in
      First, store the elements of @in into a hash (as hash keys, corresponding value doesn't matter)). Then just scan once the @unique array and, for each item, see if it exists in the hash. If if does, keep track of the index of that item, perhaps in the hash, perhaps in another array (you don't give enough details to decide what is best). This will be very fast (and, since this is an O(n) complexity, n being the size of the @unique array, it is probably not possible to find a faster algorithm, because you can't skip reading both arrays at least once, so that, at best, you'll improve details).
      I want to return each elements which between two consecutive indexes (@idxs) if 10< Length > 5.
      Sorry, I don't understand your second requirement.
      This quick "one-liner" (actually a "six-liner") gives a rough implementation of the idea in my previous post:
      $ perl -E ' > my @unique = (1, 20, 3, 4, 44, 55, 66, 77, 5, 10, 2, 11, 20, 42, 30, + 31, 32, 33, 34, 35, 36, 37, 40); > my @in = (4, 3, 2, 2, 42, 40); > my %lookup = map { $_ => 1 } @in; > for my $i (0..$#unique) { > say $i if exists $lookup{$unique[$i]}; > } > ' 2 3 10 13 22
      Of course, this does not deal with duplicates in either array, but you haven't said how they should be processed.