About Kent Cowgill
Articles filed under...
abs ab_ripper andylester arms back baggyshorts bestpractices biceps bike birthday blog bugs bus calculator cardio catalyst cgi chart chest chinups code cpan datamodel dbi doctor documentation exercise exhaustion fitness flattire flat_tire google gps heart_rate helmet history home houston html humor journal kate kenpo kenpo_x kettlebell knees lazy legs lisa lisanne maps math matthew michaelmckenna mom montreal motivation movie mysql oops orm P90X pain park patellar_tendonitis patrick pdf perl phb photos physical_therapy plyometrics poor_gait presentation procrastination progress pullups pushups pyramid rabbits racecondition rant refactor rest ribs ride route running shoulders situps slides sore spike sql statistics syntax test testing textile timex training triceps ups versioncontrol video vim vimrc walk warren work workouts yapc yapcna2007 yoga youtube

A R C H I V E S

(3)
(1)
(3)
(2)
(7)
(15)
(16)
(25)
(3)
(4)
(2)
(4)
(11)
(1)
(1)
(3)
(2)
(2)
(10)
(5)
(2)
(3)
(4)
(9)
(21)
(3)
(3)
(1)
(6)
(4)
(1)
(4)
(3)
(2)
(1)


    Is Kent Cowgill Online?
    View Kent Cowgill's profile on LinkedIn
    Add to Technorati Favorites

    Recent Entries...

    Re: Re: Yoga kicks my butt

    Chris @ 46: Tatyana @ 21 – at best its a stop gap measure...

    Re: Vibram FiveFingers FTW

    Hats off to whoever wrote this up and potesd it....

    Re: A little more detail on using a new model

    百度 [url=http://www.sina.com]sina[/url] ...

    Re: Catching up through week 7

    testing video ...

    Re: Porting a non-Moose object to Moose

    Wow, look what I found, greedy genius ...

    Re: Porting a non-Moose object to Moose

    Kevin, You're right, that does seem a little confusing. ...

    Re: Porting a non-Moose object to Moose

    Wait. I'm confused. Moose isn't the tool to reach for. So...

    Re: Porting a non-Moose object to Moose

    You should switch to MooseX::Types to declare your Typed and...

    Porting a non-Moose object to Moose

    I'm currently working with a lot of legacy code in an envi...

    Testing strategy for mocking code

    I keep finding myself using the following idiom for writing ...

    weblog | `web·lôg -läg |
    noun
    Another term for BLOG
    ORIGIN 1990s: from web in the sense [World Wide Web] and log in the sense [regular record of incidents.]
    blog | bläg |
    noun
    A web site on which an individual or group of users produces an ongoing narrative.
    ORIGIN a shortening of WEBLOG.

    Old code review

    Kent Cowgill

    Going through some old code reviews, I found the following in some of my notes.

    The idea was that there's a hunk of code that needs to produce a sorted and unique list of items. That description immediately brings to mind a few data types - an array and a hash - and a sort of some kind or another.

    The code I encountered looked like this:

    my @pre_sorted = $s->arrayOfArrays;
      my $unsorted = \@pre_sorted;
      my @key_list = ();
      my @sorted = ();
      my %hash;
      # make the AoA into a hash, keyed on [1], the name
      foreach(@$unsorted){
        $hash{$_->[1]} = $_->[0];
      }
      # copy the keys into a sorted array
      @key_list = sort( keys(%hash));
      # build the sorted AoA
      foreach(@key_list){
        my @temp_array = ( $hash{$_} , $_ );
        push @sorted, \@temp_array;
      }

    Don't get me wrong - that does do what it's supposed to do. But does it do it efficiently? Not really - that can all be replaced by the following:

    my %unique_items = ();
      my @sorted = sort { $a->[1] cmp $b->[1] }
                   grep { ! $unique_items{$_->[1]}++ }
                   $s->arrayOfArrays;

    Which has the following benefits:

    1. Much more succint.
    2. Many fewer temporary variables.
    3. More perlish.
    4. Quite a bit faster.

    How much faster? It depends a little on the size of the data passed to it. I found that most of the time, the data coming to it was very very small, but occasionally would have much larger data sets to sort. I recall having some spare time and more curiousity then is likely good for me, so I set up some benchmarks:

    First, results from running the routines
    500000 times on a tiny data set:
                    Rate    Original New version
    Original     98232/s          --        -71%
    New version 342466/s        249%          --
    
    Next, results from running the routines
    200000 times on a small data set:
                    Rate    Original New version
    Original     61920/s          --        -69%
    New version 200000/s        223%          --
    
    Now running them 10000 times on a
    larger data set:
                  Rate    Original New version
    Original    4032/s          --        -49%
    New version 7937/s         97%          --
    
    Related Photos: code

    Main Page | Login

    Do you want to buy me ? Find more gift ideas at my wishlist