arivu198314 has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks,
I'm looking someone to optimize my code
input file
picturesque liar fight nor fly 24 hours love life wrinkled a million dollars love life even plan you attempt things a million dollars many hardships many hardships married head many hardships this year secret shame present apple pie many hardships elephant careful peace many hardships apple pie good afternoon seven levels
Output is, we should count the items and which items coming more times in the input. I need to print most coming items of 5. Means, output for this input is
many hardships love life apple pie a million dollars this year
I have written a script, but its taking some time to finish this task.
#!/usr/bin/perl open(INP, '<', $ARGV[0]); while ($item=<INP>) { chomp($item); (exists $query{$item}) ? ($query{$item}=$query{$item}+1) : ($query +{$item}=1); } print join("\n", (sort {$query{$b}<=>$query{$a}} (keys %query))[0..4]) +."\n";
Basically i want to know, how to optimize this code as ninja speed
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Speed up my code
by toolic (Bishop) on Dec 14, 2011 at 13:22 UTC | |
|
Re: Speed up my code
by BrowserUk (Patriarch) on Dec 14, 2011 at 13:46 UTC | |
by Tux (Canon) on Dec 14, 2011 at 14:50 UTC | |
by BrowserUk (Patriarch) on Dec 14, 2011 at 14:54 UTC | |
|
Re: Speed up my code
by jethro (Monsignor) on Dec 14, 2011 at 13:35 UTC | |
|
Re: Speed up my code
by RichardK (Parson) on Dec 14, 2011 at 13:47 UTC | |
by arivu198314 (Sexton) on Dec 14, 2011 at 14:04 UTC | |
|
Re: Speed up my code
by locked_user sundialsvc4 (Abbot) on Dec 14, 2011 at 17:57 UTC | |
by AnomalousMonk (Archbishop) on Dec 15, 2011 at 02:59 UTC |