Shlomi Fish wrote:

in addition to the good advice that Uri gave you - some comments on your code.

On Sun, 4 Dec 2011 07:40:09 -0600
Chris Stinemetz<chrisstinem...@gmail.com>  wrote:

I have a program that I am working on improveing. The fist step I have
taken is converting it in using the strict pragma.

Now when this subroutine call is made I get the following compilation error:

Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.
Global symbol "$cell" requires explicit package name at ./evdo.pl line 279.

I understand lexical scope, but am having a hard time figuring out how to
fix this.

Any help is greatly apprciated.

Chris

sub getMarket
{
     my $_cell = shift;
     my $_mkt = "";
     my $mkt = "";
     foreach $mkt (keys %marketInfo)
     {

This is better written using «foreach my $mkt» instead of «foreach $mkt»

        my $start = $marketInfo{$mkt}->{"start"};

You should assign $marketInfo{$mkt} to a variable, or alternatively do:

        my ($start, $end) = @{$marketInfo{$mkt}}{qw(start end)};

        my $end = $marketInfo{$mkt}->{"end"};
        if( $cell>= $start&&  $cell<= $end)
        {
           $_mkt = $mkt;
           last;
        }
     }

This loop can be more succinctly expressed using first from List::Util :

return List::Util::first {
        my ($start, $end) = @{$marketInfo{$_}}{qw(start end)};
        $cell>= $start&&  $cell<= $end;
} keys(%marketInfo);

All that put aside, this lookup has O(N) complexity, and you can reduce it to
O(log(N)) by creating a

sorted array of ranges that you will binary search.
  ^^^^^^^^^^^^                         ^^^^^^^^^^^^^
   O( log N )            +              O( log N )

Or O( log 2N )

While the added efficiency MAY be faster it adds more complexity and it depends on the size of N and the algorithms implementing sort and search as to whether it will actually be faster.


We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil
-- Donald Knuth



John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction.                   -- Albert Einstein

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to