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

Replace scanner's inefficient implementation #205

Open
Paalon opened this issue Jun 17, 2024 · 0 comments
Open

Replace scanner's inefficient implementation #205

Paalon opened this issue Jun 17, 2024 · 0 comments

Comments

@Paalon
Copy link
Contributor

Paalon commented Jun 17, 2024

Current scanner uses the implementation of f1 which is inefficient.

using BenchmarkTools

n = rand(10:20, 100);
css = rand.(Char, n);
ss = String.(css);

function f1(ss::Vector{String})
    v = Any[]
    for s in ss
        push!(v, s)
    end
    string(v...)
end

function f2(ss::Vector{String})
    v = String[]
    for s in ss
        push!(v, s)
    end
    join(v)
end

function f3(ss::Vector{String})
    io = IOBuffer()
    for s in ss
        write(io, s)
    end
    String(take!(io))
end

function f4(ss::Vector{String})
    result = ""
    for s in ss
        result *= s
    end
    result
end

@btime f1($ss);
# 11.076 μs (5 allocations: 7.72 KiB)
@btime f2($ss);
# 5.659 μs (12 allocations: 17.71 KiB)
@btime f3($ss);
# 3.711 μs (8 allocations: 11.63 KiB)
@btime f4($ss);
# 22.196 μs (100 allocations: 296.73 KiB)

We need to replace it to f3.

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

No branches or pull requests

1 participant