Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow performance compared to legacy #185

Closed
monkeygroover opened this issue Dec 6, 2021 · 4 comments · Fixed by #190
Closed

Slow performance compared to legacy #185

monkeygroover opened this issue Dec 6, 2021 · 4 comments · Fixed by #190

Comments

@monkeygroover
Copy link
Contributor Author

This appears to be because rotate becomes more and more inefficient if done repeatedly on a stream, If do this:

    def rotate(value, shift) when shift == 0, do: value
    def rotate(value, shift) when not Functions.is_iterable(value), do: Enum.join(rotate(String.graphemes(to_string(value)), shift), "")
    def rotate(value, shift) when shift > 0 do
        case Enum.count value do
            0 -> []
            x -> 
                shift = rem(shift, x)
                Stream.concat(value |> Stream.drop(shift), value |> Stream.take(shift)) |> Enum.to_list
        end
    end
    def rotate(value, shift) when shift < 0 do
        case Enum.count value do
            0 -> []
            x ->
                shift = rem(shift, x)
                Stream.concat(value |> Stream.take(shift), value |> Stream.drop(shift)) |> Enum.to_list
        end
    end

It's very fast again.

Not sure if there are any downsides, going to run the tests. But using rotate on large streams probably isn't a good idea anyway.

@Adriandmen
Copy link
Owner

Yeah, there were a couple of downsides after rewriting this from Python to Elixir. Performance was indeed one of the biggest regressions.

Both versions have bugs, this one is just less buggy hahaha. Ideally I would rewrite it again in Scala or Java (or some other strongly typed language), but I can barely find the time for doing so unfortunately.

@monkeygroover
Copy link
Contributor Author

Yeah, Rust may work too (but no GC, so lots to learn)

I actually did this to get some clues: https://github.com/monkeygroover/05AB1E/tree/flame hacky but helped!

@Adriandmen
Copy link
Owner

Oohh, that is clever!

I will definitely have to check that out, thanks for sharing! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants