Skip Menu |
Report information
Id: 130982
Status: open
Priority: 0/
Queue: perl6

Owner: Nobody
Requestors: perl6 [at] mscha.org
Cc:
AdminCc:

Severity: (no value)
Tag: (no value)
Platform: (no value)
Patch Status: (no value)
VM: (no value)



To: rakudobug [...] perl.org
From: Michael Schaap <perl6 [...] mscha.org>
Subject: [PERF] "for $a..$b -> $i { ... }" loops are sometimes much slower than c-style loops
Date: Sat, 11 Mar 2017 16:06:57 +0100
Download (untitled) / with headers
text/plain 1.1k
Perl6-style simple a-to-b loops are often much slower than the corresponding C-style loops, especially when dealing with embedded loops. My "real life" example (as far as Project Euler is real life) is: http://pastebin.com/SVYAyA5z It takes about 55 seconds on my machine with C-style loops, and about 88 seconds with Perl6-style loops. (Comment the "loop" statements and uncomment the corresponding "for" statements.) A reduced example is at the bottom of this report. It outputs 111.04240975 95.56877319 on my machine. I'm using the latest Rakudo Star, 2017.01. See: https://irclog.perlgeek.de/perl6/2017-03-11#i_14245536 and below. ---------- #!/usr/bin/env perl6 sub perl6-loop($n) { my $start = now; my $sum = 0; for 1..$n -> $i { for 1..$i -> $j { $sum += $i+$j; } } say now - $start; } sub c-loop($n) { my $start = now; my $sum = 0; loop (my $i = 1; $i <= $n; $i++) { loop (my $j = 1; $j <= $i; $j++) { $sum += $i+$j; } } say now - $start; } sub MAIN($n = 10⁴) { perl6-loop($n); c-loop($n); }
To: perl6-compiler [...] perl.org, bugs-bitbucket [...] rt.perl.org
Subject: Re: [perl #130982] [PERF] "for $a..$b -> $i { ... }" loops are sometimes much slower than c-style loops
From: Lloyd Fournier <lloyd.fourn [...] gmail.com>
Date: Sun, 12 Mar 2017 05:48:55 +0000
Download (untitled) / with headers
text/plain 1.7k
If you think that discrepancy is impressive you're going to love this. I added a version to your example using native ints: https://gist.github.com/LLFourn/8c3e895e789fab957355ce23c9420133

bash-3.2$ perl6 native-int-perf.p6
perl6-loop: 84.8739988 
c-loop: 67.65849241 (1.25 times faster)
native-loop: 0.4981954 (135.81 times faster)

(!)

On Sun, Mar 12, 2017 at 2:08 AM Michael Schaap <perl6-bugs-followup@perl.org> wrote:
Show quoted text
# New Ticket Created by  Michael Schaap
# Please include the string:  [perl #130982]
# in the subject line of all future correspondence about this issue.
# <URL: https://rt.perl.org/Ticket/Display.html?id=130982 >


Perl6-style simple a-to-b loops are often much slower than the
corresponding C-style loops, especially when dealing with embedded loops.

My "real life" example (as far as Project Euler is real life) is:
http://pastebin.com/SVYAyA5z
It takes about 55 seconds on my machine with C-style loops, and about 88
seconds with Perl6-style loops.  (Comment the "loop" statements and
uncomment the corresponding "for" statements.)

A reduced example is at the bottom of this report.  It outputs

111.04240975
95.56877319

on my machine.

I'm using the latest Rakudo Star, 2017.01.

See: https://irclog.perlgeek.de/perl6/2017-03-11#i_14245536 and below.

----------

#!/usr/bin/env perl6

sub perl6-loop($n)
{
     my $start = now;
     my $sum = 0;
     for 1..$n -> $i {
         for 1..$i -> $j {
             $sum += $i+$j;
         }
     }
     say now - $start;
}

sub c-loop($n)
{
     my $start = now;
     my $sum = 0;
     loop (my $i = 1; $i <= $n; $i++) {
         loop (my $j = 1; $j <= $i; $j++) {
             $sum += $i+$j;
         }
     }
     say now - $start;
}

sub MAIN($n = 10⁴)
{
     perl6-loop($n);
     c-loop($n);
}
Date: Sun, 12 Mar 2017 10:27:13 -0400
CC: perl6-compiler <perl6-compiler [...] perl.org>, bugs-bitbucket [...] rt.perl.org
To: Lloyd Fournier <lloyd.fourn [...] gmail.com>
Subject: Re: [perl #130982] [PERF] "for $a..$b -> $i { ... }" loops are sometimes much slower than c-style loops
From: Brandon Allbery <allbery.b [...] gmail.com>
Download (untitled) / with headers
text/plain 708b

On Sun, Mar 12, 2017 at 12:48 AM, Lloyd Fournier <lloyd.fourn@gmail.com> wrote:
Show quoted text
perl6-loop: 84.8739988 
c-loop: 67.65849241 (1.25 times faster)
native-loop: 0.4981954 (135.81 times faster)

Still quite a lot of optimization to be done on that front. WRT native int, one of the issues is needing to track when boxing is/isn't needed, otherwise nativizing can be a pessimization because of constant reboxing. The optimizer isn't up to tracking boxing like that yet.

--
brandon s allbery kf8nh                               sine nomine associates
allbery.b@gmail.com                                  ballbery@sinenomine.net
unix, openafs, kerberos, infrastructure, xmonad        http://sinenomine.net


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org