Home > Raku > The next fast thing

The next fast thing

A few commits ago lizmat taught next to take an argument. I started to play with this and found that not all loops are created equal.

sub prefix:<♥>(&c) {
    LEAVE say (now - ENTER now) ~ 's';  # Don't you ♥ Raku?
    c
}

♥ { say sum gather for ^1_000_000 { .take if .is-prime; } } # 1.399831818s
♥ { say sum eager for ^1_000_000 { .&next if .is-prime; } } # 1.131352526s
♥ { say sum do for ^1_000_000 { .&next if .is-prime; } }    # 1.60557427s
♥ { say sum (^1_000_000).grep: *.is-prime; }             # 0.778440528s

I’m surprised that the do for-form is the slowest. That gather is slower leaves the question if a for-loop is the better way to create a lazy list.

my \a = lazy gather for ^100_000_000 { .take if .is-prime; }
my \b = lazy for ^100_000_000 { .&next if .is-prime; }
♥ { say sum a[^1_000_000]; } # 25.91494395s
♥ { say sum b[^1_000_000]; } # 26.521749639s

Optimising is an art of doing the same thing over and over again. There seems to be room for optimising things that do things over and over again.

Categories: Raku
  1. No comments yet.
  1. July 12, 2021 at 20:40

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: