[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] Ineffektiv perlcode





Peter Makholm wrote:

Peter Maersk-Moller <sslug@sslug> writes:



----------------------
1 my $html_header = "et eller andet";
2 my $html_body = "";
3 my $html_tail = "et eller andet";
4 my $sth = $dbh->prepare("SELECT SOMETHING"); //bare et MySQL-udtræk
5 $sth->execute();
6 while (my $ref = $sth->fetchrow_arrayref) {
7   $html_body = $html_body.$$ref[0]."<br>";
8 }
9 print STDOUT $html_header.$html_body.$html_tail;
----------------------

Som i kan se i linje 7 kopierer jeg en streng over i sig selv og jo længere
strengen bliver jo længere vil hver iteration tage.



Jeg ville nok skrive linje 7 således:


$html_body .= $$ref[0]."<br>";

Jeg ved dog ikke om perl vil gøre noget mere effektivt af den grund.

En anden mulighed kunne være at opsamle i et array istedet for i en
streng. Det vil sige enten:

push @html_body, $$ref[0]."<br>";

eller

$html_body[$#html_body + 1] = $$ref[0]."<br>";

Det første burde nok være hurtigst og standardmåden. Udskriften ville
så gøres:

print $html_header, @html_body, $html_tail;


Men du må nok benchmarke for at se om det er en forbedring af betydning.





Hej. Jeg er aldeles ikke Perl-kyndig, men kan godt forestille mig at meget lange


strenge vil koste tid til at søge enden. Ville det ikke bare blive enormt meget
hurtigere at skrive data ud med det samme de findes. Måske er der en eller anden
skjult grund til at opsamle dem først, men er der ikke det, så skriv dem dog med
det samme

/ Søren H. Sørensen



 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-10-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *