The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-12-2005
sekar sundaram sekar sundaram is offline
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..
  #2 (permalink)  
Old 11-13-2005
cbkihong cbkihong is offline Forum Advisor  
Advisor
  
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,624
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 07:58 AM..
  #3 (permalink)  
Old 11-13-2005
sekar sundaram sekar sundaram is offline
Registered User
  
 

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

thats interesting...
got great ideas...
thanks..
Closed Thread

Bookmarks

Tags
perl, perl shift, shift, shift perl

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0