Posted by: songbard | August 23, 2008

Perl : A foreach loop normally saves time!

Script:

#!/usr/bin/perl

# use Benchmark module
use Benchmark qw (:all);

# time 3 different versions of the same code
cmpthese (1000, {
‘foreach’ => ‘foreach $x (1…5000)
{
sin($x/($x+2));
}’,
‘while’ => ‘$x=1;
while ($x <= 5000)
{
sin ($x/($x+2));
$x++;
}’,
‘for’ => ‘for ($x=1; $x<=5000; $x++)
{
sin ($x/($x+2));
}’,
});
…………………………..
…………………………..

Result:

Rate for while foreach
for 400/s — -2% -17%
while 407/s 2% — -16%
foreach 483/s 21% 19% —

Advertisements
Posted by: songbard | August 23, 2008

Perl : Checking existing modules in CPAN

perl -MLWP::Simple -e “getprint ‘http://cpan.org/modules/01modules.index.html'&#8221; | perl -pe ‘s/.*<;a//; s/.*?>;(.*?)<;\/a>;.*$/$1/’ | grep Mechanize

……….
……….

Bundle-WWW-Mechanize-Shell-0.29.tar.gz
WWW-Mechanize-Shell-0.44.tar.gz
Mozilla-Mechanize-GUITester-0.15.tar.gz
Mozilla-Mechanize-0.05.tar.gz
……….

Posted by: songbard | August 23, 2008

Sybase :: forcing index in a SQL query

Sometimes, it becomes necessary to force an index in a query when optimizer does not follow indexing structure goes for a full table scan.

Supposing Indx I1 is indexed on columns EMP_NAME, EMP_PHONE from EMPLOYEE table.

Now the following types of query would go for full table scan:

1. select * from EMPLOYEE where EMP_NAME=<SOMENAME>;
2. select * from EMPLOYEE where EMP_PHONE=<SOMEPHONE>;

3. select * from EMPLOYEE where EMP_NAME in (SOMERANGE) and EMP_PHONE=<SOMEPHONE>;

4. select * from EMPLOYEE where EMP_NAME =<SOMENAME> and EMP_PHONE in (SOMERANGE)

In the above queries if with tablename we specify the index to follow, queries avoid the full table scan.
eg.
select * from EMPLOYEE(index I1) where EMP_NAME =<SOMENAME> and EMP_PHONE in (SOMERANGE).
OR
select * from EMPLOYEE(1) where EMP_NAME =<SOMENAME> and EMP_PHONE in (SOMERANGE).

where 1 is the index no. of I1 for EMPLOYEE table.

But am not aware of any flipside of this approach……

« Newer Posts

Categories