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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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
LEARN ABOUT XFREE86
shift
shift(1) User Commands shift(1)NAME
shift - shell built-in function to traverse either a shell's argument list or a list of field-separated words
SYNOPSIS
sh
shift [n]
csh
shift [variable]
ksh
* shift [n]
DESCRIPTION
sh
The positional parameters from $n+1 ... are renamed $1 ... . If n is not given, it is assumed to be 1.
csh
The components of argv, or variable, if supplied, are shifted to the left, discarding the first component. It is an error for the variable
not to be set or to have a null value.
ksh
The positional parameters from $n+1 $n+1 ... are renamed $1 ..., default n is 1. The parameter n can be any arithmetic expression that
evaluates to a non-negative number less than or equal to $#.
On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways:
1. Variable assignment lists preceding the command remain in effect when the command completes.
2. I/O redirections are processed after variable assignments.
3. Errors cause a script that contains them to abort.
4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari-
able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not
performed.
ATTRIBUTES
See attributes(5) for descriptions of the following attributes:
+-----------------------------+-----------------------------+
| ATTRIBUTE TYPE | ATTRIBUTE VALUE |
+-----------------------------+-----------------------------+
|Availability |SUNWcsu |
+-----------------------------+-----------------------------+
SEE ALSO csh(1), ksh(1), sh(1), attributes(5)SunOS 5.10 15 Apr 1994 shift(1)