scripting....guru's help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting scripting....guru's help needed
# 1  
Old 11-12-2005
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  
Old 11-13-2005
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  
Old 11-13-2005
thanks cbkihong,

thats interesting...
got great ideas...
thanks..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Scripting Help needed

Hi folks, need a small help lets say i have a text file with the following content: james tom jack spielberg i want to append text to the beginning of each line with a variable(lets says FirName:) and i want to combine all the names with a space in between and store it in another... (11 Replies)
Discussion started by: tech_frk
11 Replies

2. Shell Programming and Scripting

Help needed in scripting

Hi Guys, I need help in scripting out the below : this is a sample data i have in my file: jobname type 8:00:00 AM I need to remove the ":00" from the time field alone. Thanks in advance for all ur help (8 Replies)
Discussion started by: a12ka4
8 Replies

3. Shell Programming and Scripting

Scripting help needed

Hi All, I have a conf file and it has two entries seperated by comma, look like :- best1,ls /opt/bmc/Patrol3/*/best1 ......, ....................... In which "Best1" is the product name and "ls /opt/bmc/Patrol3/*/best1" is the way to find the product version of Best1 in that particular... (5 Replies)
Discussion started by: Renjesh
5 Replies

4. Shell Programming and Scripting

regexp: really tired, guru help needed...

Hello all! Please help me with the following complex regexp which works in egrep: egrep '\"http:\/\/ccc\.bbb\.com\/documents\/0000\/{4}\/(+\.{3})*\"' my-file.htmlBut silently does not work in sed: sed "s/\"http:\/\/ccc\.bbb\.com\/documents\/0000\/{4}\/(+\.{3})*\"/aaa/g" my-file.htmlor sed... (7 Replies)
Discussion started by: ulrith
7 Replies

5. Homework & Coursework Questions

Scripting needed

Hi, My task is to check the file test.txt every 15 min from Mon-Fri 9:00AM - 6:00PM. We get this file from our mainframes, every 15 min it will update the same file. My task is to compare file timestamp with current system time stamp and check if the file is updated or not. If the file doesn't... (0 Replies)
Discussion started by: chinniforu2003
0 Replies

6. Shell Programming and Scripting

Help needed with scripting.

Hello Friends, I am very new to scripting and currently have come across a situation where I need to create a UNIX script which would look for certain text in a file and then email me if it finds it. I am trying to trouble shoot an issue with our internet websites and I need to know when I... (1 Reply)
Discussion started by: mahive
1 Replies

7. Shell Programming and Scripting

sh scripting! Help Needed

Hello fellas or ladies. I am new on this site and new to the unix operating system. I have been working with UNIX and i love it so far, i learned some stuff and most of the beneficiary command but I need help renaming all the files in my directory and doing them one by one is just tiring.... (1 Reply)
Discussion started by: keyboardkowboy
1 Replies

8. Shell Programming and Scripting

little scripting help needed!!

guys i need bit of help!! i am writing a script which finds files that have not been accessed for a no of days and delete those files...the no of days value is inputted at the command line.... i am using the following : find $1 -atime +7 -exec rm -i in the second step i want to copy all... (2 Replies)
Discussion started by: vats
2 Replies

9. Shell Programming and Scripting

scripting guru's pls help me with scripting on AIX

can someone pls help me with the script for a files coming from one system to a particular directory and i want to write a script to move those files to another directory on different system by renaming the files... pls someone help me on this... thanking in anticipation.... (1 Reply)
Discussion started by: thatiprashant
1 Replies

10. UNIX for Dummies Questions & Answers

DNS guru needed...

Well I have a new hosting account. I love these guys, because they have everything I need, and give me everything I need, but at the cost of I must configure it myself. I am trying to create a subdomain, and they use raw zone access for dns administration. I have no idea how to configure the zone... (1 Reply)
Discussion started by: btmitch
1 Replies
Login or Register to Ask a Question