Skip to content

Performance issues with sequence handling, bug? #1200

@benthor

Description

@benthor

Hi! Wren is a great little language! (And I say this despite my strong background in Lua...)

I've had some fun trying out various approaches of the Sieve of Eratosthenes to find prime numbers, when I noticed something odd.

Most sieve implementations tend to speed up as they fill up the range. One Example is the following list-based code:

var sieve = List.filled(99999998, true)

for (i in 2...sieve.count) {
        if (sieve[i]) {
                System.print(i)
                var mult = i
                while (mult < sieve.count) {
                        sieve[mult] = false
                        mult = mult + i
                }
        }
}

When you run this, you'll notice how the first few primes take a while to print, before execution speeds up and the numbers come faster and faster.

With the following sequence-based implementation, it's the other way around however:

var seq = 2..99999997

while (!seq.isEmpty) { 
        //don't know a better way to peek at the first element in a sequence
        var elem=seq.take(1).toList[0] 
        System.print(elem)
        seq = seq.where{ |n| n % elem != 0 }
}

Something is very wrong with the output speed of that code. It starts printing quite quickly but then it slows to a crawl. Any ideas?

I'm using wren 0.4.0 on x86_64 Linux.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions