Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

    require "benchmark"
    n = 100_000

    A = "foo " * 20
    B = "bar " * 20
    C = "baz " * 20
    D = "foobar " * 20
    STRINGS = [A, B, C, D]

    Benchmark.bm(4) do |x|
      x.report("<<") do
        n.times do
          "" << A << B << C << D
        end
      end

      x.report("join") do
        n.times do
          STRINGS.join
        end
      end
    end


               user     system      total        real
    <<     0.090000   0.010000   0.100000 (  0.102411)
    join   0.070000   0.010000   0.080000 (  0.071142)


Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: