[JOB] Perl Software Developer and Database programmer
Dirk Koopman
djk at tobit.co.uk
Wed Feb 22 17:34:55 GMT 2006
On Wed, 2006-02-22 at 08:10 -0800, Ovid wrote:
> As a thoroughly depressing example, here's a sample question I throw at
> people who apply for Perl jobs:
>
> Imagine we're building an app and it turns out to be slow. We've
> profiled the code and reduced the problem down to this:
>
> foreach my $alpha ( @alphas ) {
> foreach my $beta ( @betas ) {
> if ( $alpha == $beta ) {
> push @results, $beta;
> }
> }
> }
>
> 1. What is a likely reason for the performance problem?
> 2. How might we speed it up?
> Heck, if you've never used Perl, the problem itself should be clear,
> even if a "Perlish" solution is not (sort @betas and do a binary
> search, anyone?)
>
Ok I'll bite (I'm bored waiting for Airwave to sort their network out).
How about:
my @beta = (1,3,5,6,7,9);
my @alpha = (2,3,4,8,9,10);
my %beta;
@beta{@beta} = ();
my @results = grep {exists $beta{$_}} @alpha;
print join(',', @results), "\n";
Dirk
More information about the london.pm
mailing list