The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts 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 04:50 PM
SHell Scripting Help Needed cskumar Shell Programming and Scripting 2 07-16-2006 10:55 PM
Help needed - shell scripting garric Shell Programming and Scripting 8 05-23-2006 03:08 AM
scripting guru's pls help me with scripting on AIX thatiprashant Shell Programming and Scripting 1 01-20-2006 04:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 11-12-2005
Registered User
 

Join Date: Jul 2005
Location: banagalore
Posts: 44
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..
Reply With Quote
Forum Sponsor
  #2  
Old 11-12-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,477
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)
}
The program generates 50000 items and then try to randomly generate another set of 1000 items to search for in these 50000 items. I expect most if not all of them will not match. Here we comment out line (2) as shown leaving (1) open, and use time() to time the result on my Windows XP system (with cygwin):

$ 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 04:58 AM.
Reply With Quote
  #3  
Old 11-13-2005
Registered User
 

Join Date: Jul 2005
Location: banagalore
Posts: 44
thanks cbkihong,

thats interesting...
got great ideas...
thanks..
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:08 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0