|
|
|
|
|
|
|
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#14 | |
|
If something's hard to do, then it's not worth doing
Join Date: Sep 2008
Location: Berlin, Germany
Posts: 247
|
Quote:
Practical example being, suppose I have an array full of, say, usernames, and I want to de-dupe them. There's at least 2 ways to do it: Code:
# usernames
my @usernames = ('a'..'z','a'..'z'); # a thru z, twice
# method 1
sub method_one {
my %usernames = (map { $_ => 1 } @usernames);
print sort keys %usernames;
}
# method 2
sub method_two {
my %usernames = ();
$usernames{$_}++ for(@usernames);
print sort keys %usernames
}
Code:
Rate Method #1 Method #2 Method #1 16181/s -- -41% Method #2 27624/s 71% -- Another fun one; say you have to http escape a string by turning the string into it's own hex interpretation, the one I see most (I've asked this question to applicants at job interviews) is this: Code:
my $string = 'http://someurl/'; printf "%02X", $_ for(split //, $string); Code:
my $string = 'http://someurl/';
printf '%02X' x length($string), map { ord $_ } (split //, $string);
Code:
one 53763/s -- -16% two 64103/s 19% -- Now, for fun, let's both solve this problem: Write a perl script that outputs the numbers from 1 to 100, 10 per line, zero-padded. Example output: Code:
001 002 003 004 005 006 007 008 009 010 011 012 013 014 015 016 017 018 019 020 . . . . And sorry for the thread hijack ![]() |
|
|
|
|
|
|