Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Speeding up

Reply
Thread Tools

Speeding up

 
 
gamo
Guest
Posts: n/a
 
      02-19-2006

I want to speed up this code:

$kk=0;
for (0..7){
$kk++ if (index($k,$res[$_]) >=0);
}
if ($kk=={
...

TIA

--
http://www.telecable.es/personales/gamo/
 
Reply With Quote
 
 
 
 
Brian Wakem
Guest
Posts: n/a
 
      02-19-2006
gamo wrote:

>
> I want to speed up this code:
>
> $kk=0;
> for (0..7){
> $kk++ if (index($k,$res[$_]) >=0);
> }
> if ($kk=={
> ...
>
> TIA
>



You haven't told us what is in $k or @res. But in any case, index will
likely be the fastest way to do it.

Using != -1 instead of >=0 might buy you a millionth of a second.


--
Brian Wakem
Email: http://homepage.ntlworld.com/b.wakem/myemail.png
 
Reply With Quote
 
 
 
 
Anno Siegel
Guest
Posts: n/a
 
      02-19-2006
gamo <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> I want to speed up this code:
>
> $kk=0;
> for (0..7){
> $kk++ if (index($k,$res[$_]) >=0);
> }
> if ($kk=={
> ...


Try (untested)

unless ( grep index( $k, $res[ $_]) < 0, 0 .. 7 ) {

Benchmark it. It is shorter, it may or may not be faster.

Anno

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      02-19-2006
On Sun, 19 Feb 2006, Anno Siegel wrote:

> Try (untested)
>
> unless ( grep index( $k, $res[ $_]) < 0, 0 .. 7 ) {
>
> Benchmark it. It is shorter, it may or may not be faster.
>
> Anno


#!/usr/local/bin/perl -w

use Benchmark 'cmpthese';

$k = "11-12-13-14-15-16-17-18-";
@res = qw( 11 12 13 14 15 16 17 1;


cmpthese -5, {
'for' => sub {
$kk=0;
for (0..7){
$kk++ if (index($k,$res[$_]) >=0);
}
if ($kk=={
;
}
},
'grep' => sub {
unless ( grep index( $k, $res[ $_]) < 0, 0 .. 7 ) {
;
}
},
};

__END__



Rate for grep
for 167564/s -- -36%
grep 261988/s 56% --

Thank you for the responses. The problem was more related with $k.
Doing

for $i (){
for $j (){
for $k (reverse @combinations){
code;
}
}
}

the thing was severely improved.

Thank you for your responses.

Cheers.

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-19-2006
gamo <(E-Mail Removed)> wrote in newsine.LNX.4.64.0602190836490.27545
@jvz.es:

> I want to speed up this code:
>
> $kk=0;
> for (0..7){
> $kk++ if (index($k,$res[$_]) >=0);
> }
> if ($kk=={


Have you measured where your program actually spends it time?

Well, here are some meaningless results:

#!/usr/bin/perl

use strict;
use warnings;

my $s = <<EO_TEXT;
gamo posted a question about making a meaningless piece of code
faster. He did not provide any sample data nor did he provide a
short but complete script others can run. It looks like gamo has
not read the posting guidelines. Please do.
EO_TEXT

my @r = qw( gamo post question provide FAQ short incomplete don't);

sub gamo_index {
my $kk = 0;
for (0 .. 7) {
$kk++ if 0 <= index $s, $r[$_];
}
}

sub gamo_grep { scalar grep { 0 <= index $s, $_ } @r }

use Benchmark qw( cmpthese );

cmpthese -1, {
gamo_index => \&gamo_index,
gamo_grep => \&gamo_grep,
};

__END__

D:\Home\asu1\UseNet\clpmisc\faster> f
Rate gamo_index gamo_grep
gamo_index 57389/s -- -34%
gamo_grep 86624/s 51% --

However, a meaningful optimization would look at the overall problem you
are trying to solve. Trying to make individual lines go faster is
generally meaningless.

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html

 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      02-19-2006
On Sun, 19 Feb 2006, Abigail wrote:

> gamo ((E-Mail Removed)) wrote on MMMMDLV September MCMXCIII in

September?
....
> The obvious improvement is to stop testing as soon as one test
> fails, as you only want to do something if all cases match.
> You'd have to benchmark with data that's relevant for your application
> (and test both data that matches *and* data that fails).


Ok, but I can't test all the real problem, which ranges varies too much.

>
> One way of writing would be:
>
>
> if (index ($k => $res [0]) >= 0 &&
> index ($k => $res [1]) >= 0 &&
> index ($k => $res [2]) >= 0 &&
> index ($k => $res [3]) >= 0 &&
> index ($k => $res [4]) >= 0 &&
> index ($k => $res [5]) >= 0 &&
> index ($k => $res [6]) >= 0 &&
> index ($k => $res [7]) >= 0) {
> ...
> }
>
>
> For engineering purposes not the best, but you lose the overhead of
> a loop (and no additional scopes to enter), and gain the benefit of
> skipping the rest of the tests if one fails.
>
>
>
> Abigail
> --
> perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'
>


Thank you very much. We have a winner

Rate for grep if
for 163424/s -- -36% -43%
grep 254970/s 56% -- -11%
if 285632/s 75% 12% --

The problem as I said before is not yet solved.
Reversing the matrix of $k only speed up initial calculations.
Thus, it's neccesary to optimize this part of code, which is
executed millions and millions times.

Best regards

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-19-2006
>>>>> "g" == gamo <(E-Mail Removed)> writes:

g> I want to speed up this code:

g> $kk=0;
g> for (0..7){
g> $kk++ if (index($k,$res[$_]) >=0);
g> }
g> if ($kk=={
g> ...

my main suggestion is to loop over the @res values directly and not
index to get them. you don't seem to really need the value of $kk (and
it will never get to 8 with a 0 .. 7 loop so that is a bug!)

if you must have a check for failure you could use a flag (eww) or wrap
the test in a sub like this (untested):

sub check_for_foolish_value {

my $text = shift ;

foreach my $foolish_value ( @_ ) {

return $foolish_value if
index( $text, $foolish_value ) >= 0 ;
}

return ;
}

i bet even with the sub overhead it will be faster than all that array
indexing. but i could be wrong so someone else should benchmark it.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      02-19-2006
On Sun, 19 Feb 2006, Uri Guttman wrote:

>>>>>> "g" == gamo <(E-Mail Removed)> writes:

>
> g> I want to speed up this code:
>
> g> $kk=0;
> g> for (0..7){
> g> $kk++ if (index($k,$res[$_]) >=0);
> g> }
> g> if ($kk=={
> g> ...
>
> my main suggestion is to loop over the @res values directly and not


That could be fine.

> index to get them. you don't seem to really need the value of $kk (and
> it will never get to 8 with a 0 .. 7 loop so that is a bug!)
>


It is not. From 0 to 7 there are 8 evaluations.


> if you must have a check for failure you could use a flag (eww) or wrap
> the test in a sub like this (untested):
>
> sub check_for_foolish_value {
>
> my $text = shift ;
>
> foreach my $foolish_value ( @_ ) {
>
> return $foolish_value if
> index( $text, $foolish_value ) >= 0 ;
> }
>
> return ;
> }
>
> i bet even with the sub overhead it will be faster than all that array
> indexing. but i could be wrong so someone else should benchmark it.
>
> uri
>
> --
> Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
> --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
> Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
>

Best regards.


--
http://www.telecable.es/personales/gamo/
Sólo hay 10 tipos de personas, las que saben binario y las que no
GED/GB d+ s+:+ a C++ U+++ P+++ L++ E---- W++ N++ o K+ w O+ M- V
PS++ PE++ Y PGP+ t 5-- X+ R-- tv-- b++ DI++ D+ G- e+++ h+ r-- z
perl -e 'print 111_111_111**2,"\n";'
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-19-2006
>>>>> "g" == gamo <(E-Mail Removed)> writes:

g> $kk=0;
g> for (0..7){
g> }
g> if ($kk=={
g> ...

>> index to get them. you don't seem to really need the value of $kk (and
>> it will never get to 8 with a 0 .. 7 loop so that is a bug!)
>>


g> It is not. From 0 to 7 there are 8 evaluations.


i never said the number of evaluations wasn't 8. i said $kk will never
be 8 so the if ($kk== will always fail. that is a bug. a classic off
by one or picket fence error. in a c style for loop, $kk would probably
become 8 so it would fail of the loop. in foreach ( $n .. $m ) loops the
loop iterator gets $m as its last value.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      02-19-2006
>>>>> "A" == Abigail <(E-Mail Removed)> writes:


A> for (0 .. 7) {
A> $kk ++;
A> }
A> if ($kk == {
A> print "uri is wrong\n";
A> }


A> __END__
A> uri is wrong

blech, i forgot about the $kk++ in the OP's code. it's been a couple of
days since i saw it.

uri

--
Uri Guttman ------ (E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ot]Valid excuse for speeding? JaR MCSE 9 01-05-2005 08:23 PM
speeding up data transfer? Devin Panchal Wireless Networking 1 09-06-2004 05:46 PM
Need some hints on speeding up Spamtrap Perl 1 08-11-2004 11:25 PM
Speeding up pages OHM ASP .Net 2 05-24-2004 03:24 PM
Speeding up page display Troy ASP .Net 2 01-22-2004 09:13 PM



Advertisments