S
S
Salavat Sitdikov2015-04-08 09:49:48
Perl
Salavat Sitdikov, 2015-04-08 09:49:48

Who has experience in optimizing Perl scripts?

Good morning. Faced the following problem.
There is a project which code is written on Perl. I supported it for a long time, but now the trouble has come - support rests on system resources.
The person who wrote this code is offline.
The essence is the following, there is a
sub treecom {} which displays comments. recursively. But requests to a DB - in each iteration.
I can write in php and fix it - but it's not clear how to embed the code, so the following help is needed.
We need the backbone of a function that would look like this in PHP:

function getcomments($id){
$tree = array();
$comments = Db::getCommentsByArticle($id);
foreach ($comments as $row){
$tree[$row->parent][] = $row;
}
return getTreeComments($tree, 0);
}

function getTreeComments($tree, $parent){
$html = '';
if (isset($tree[$iter])){
$html  .= $row->comment;
if (isset($tree[$row->id])){
$html .= getTreeComments($tree, $row->id);
}
return $html;
}

The code itself is in Perl
sub treecom {
    ( my $przd, my $y, my $allend ) = @_;
    $cntr++;
    if ( $cntr > 200 ) { print qq[<b>Зацикливание!!</b><br><hr>$bb]; exit; }
    $y++;
    $selectp = "SELECT * FROM table WHERE key = value ORDER BY date ASC, id ASC
    ";
    my $sp = $dbh->prepare($selectp);
    $sp->execute;
    while (
        ( $arid, $namek, $titlek, $textk, $date, $end, $usr_id, $rait, $pub, $usr_name, $status, $src ) =
        $sp->fetchrow_array )
    {
        undef $img;

        if ($src) {
          $img = qq[<img src="/blog/users/$usr_id/profile.$src" width="30px" align="left" hspace="5" />];
        }
        if ( $pub eq "0" && $status < 1 ) { next; }
        $date =~ /(\d\d\d\d)-(\d\d)-(\d\d) (\d\d:\d\d:\d\d)/;
        $dates = qq[$3.$2.$1];
        $time  = $4;
        @tm    = split( ':', $time );
        $time  = qq[$tm[0]:$tm[1]];
        $plft  = ( $y * 10 ) . "px";

        if ( !$usr_name ) { $namek = qq[<font class="nicname">$namek</font>]; }
        else {

          $namek = qq[<div style="width: 50%; float: left;">$img<a href="/reader/$usr_id/" class="nicnamea"><b>$usr_name</b></a>&nbsp;&nbsp;<font class="gryl">$usr_cnt</font></div>];
        }

        $bb .= qq[<tr><td style="padding: 0 0 10px $plft"><a name="cmt$arid"></a>$namek

         <div style="clear: both;"></div>
         <div>$textk</div>
        ];
        
        if ( $end eq "1" && ( $allend eq 0 || $allend >= $y ) ) {
            $allend = $y;
        }
        if ( $allend > $y || $allend eq "0" ) {
            $otv = qq[<a href="/cgi-bin/comment.pl?article=$article&amp;trs=table&amp;parcom=$arid#cform" class="icomm" onclick="atcomm('$arid','table');">[ответить на комментарий]</a>];
        }
        $bb .= qq[<div style="float: left;">$otv</div><div style="float: right;"><font class="gryl">$dates $time</font></div></td></tr>];
        undef $otv;
        $par =
          $dbh->selectrow_array("SELECT id FROM table where parent='$arid'");
        if ($par) { treecom( $arid, $y, $allend ); }
        if ( $y eq $allend ) { $allend = 0; }
    }

    $sp->finish;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
pcdesign, 2015-04-08
@zona7o

my %child;
my $n = $dbh->prepare('SELECT * FROM `table` where key =?  ');
$n->execute($value);
my $all = $n->fetchall_arrayref( {} );
foreach my $item (@$all) {
    print $item->{'arid'};
}

F
FanatPHP, 2015-04-08
@FanatPHP

And is it true that the problem is "in each iteration"? Have you profiled the script?
How many of those comments are there? Hundred? Two hundred? For indexed queries, this is nonsense.
The reasons are very different. You won't optimize much with the poke method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question