![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| little scripting help needed!! | vats | Shell Programming and Scripting | 2 | 09-02-2007 07:50 PM |
| SHell Scripting Help Needed | cskumar | Shell Programming and Scripting | 2 | 07-17-2006 01:55 AM |
| Help needed - shell scripting | garric | Shell Programming and Scripting | 8 | 05-23-2006 06:08 AM |
| scripting guru's pls help me with scripting on AIX | thatiprashant | Shell Programming and Scripting | 1 | 01-20-2006 07:58 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
scripting....guru's help needed
perl guru's,
what i think is, not simply writing a script...i like to write a script which must take less time to run and less memory use..i like to fine tune my program... for ex, my program has a subroutine(1 file open &file close and 1print ) 2if loops(1 file open &file close and 3print ) and a while loop with split, chomp 3system cmd's, 2if loops..like that.. i like to know: 1).in perl what statements will use less cpu, memory, time to run... what loops or statments or what are all the things one programmer should try to avoid and what should be included...what r all the best practices?... 2). is that same for perl in both unix and windows?.. 3).whether those rules or ideas r same to unix shell scriptiing or not? thanks in advance.. |
|
||||
|
The FAQ has two Q&A on performance tuning:
http://perldoc.perl.org/perlfaq3.htm...-run-faster%3f http://perldoc.perl.org/perlfaq3.htm...less-memory%3f Whether you can make a lot of difference with such advice I doubt it, as in my opinion Perl is already very efficient provided you do not use a particularly dumb way to do something. And those advice do not generally make much sense unless you are really familiar with the internals of Perl. But there are a few obvious observations, though. For example, sequential search for an item in an array is dumb because you need to search for all items before you can tell an item is not in the array. If you use a hash to store the items as keys in the first place, you will get significant performance improvements. This benchmark test tells you so: Code:
#!/usr/bin/perl -w
sub randstr {
my $num = shift;
my $str = '';
for (my $i = 0; $i < $num; $i++) {
$str .= chr(int(rand(26)) + 65);
}
return $str;
}
sub str_exists_in_array(\@$) {
my ($array, $str) = @_;
for (@$array) {
if ($str eq $_) {
return 1;
}
}
return 0;
}
sub str_exists_in_hash(\%$) {
my ($hsh, $str) = @_;
return exists $$hsh{$str};
}
srand();
my @array = ();
my %hsh = ();
for (my $i = 0; $i < 50000; $i++) {
my $str = randstr(20);
push(@array, $str);
$hsh{$str} = 1;
}
# Query 1000 times
for (my $i = 0; $i < 1000; $i++) {
my $strToTest = randstr(20);
str_exists_in_hash(%hsh, $strToTest); # (1)
# str_exists_in_array(@array, $strToTest); # (2)
}
$ time perl test1.pl real 0m0.895s user 0m0.015s sys 0m0.015s Good. Less than 1 sec. Now with the line (1) commented out but not (2): $ time perl test1.pl real 4m14.705s user 0m0.015s sys 0m0.015s The search now takes more than 4 minutes to complete. The two only differ in the way of searching. The data generation step is common for both tests. So we can conclude using a bad way to search can hurt performance when the number of searches is large. Last edited by cbkihong; 11-14-2005 at 07:58 AM.. |
![]() |
| Bookmarks |
| Tags |
| perl, perl shift, shift, shift perl |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|