From 533070bfc69cae5b09103a229e4db33b6af695cb Mon Sep 17 00:00:00 2001 From: Kazuhiro NISHIYAMA Date: Mon, 3 Jun 2019 10:32:32 +0900 Subject: [PATCH] Use lines instead of split MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` % cat ~/bench-split.yml prelude: | s = "foo\nbar\nbaz\n" benchmark: '/(?<=\n)/': | s.split(/(?<=\n)/) '/^/': | s.split(/^/) 'lines': | s.lines Warming up -------------------------------------- /(?<=\n)/ 459.123k i/s - 467.844k times in 1.018994s (2.18μs/i) /^/ 467.922k i/s - 469.744k times in 1.003894s (2.14μs/i) lines 2.343M i/s - 2.424M times in 1.034677s (426.84ns/i) Calculating ------------------------------------- /(?<=\n)/ 422.347k i/s - 1.377M times in 3.261232s (2.37μs/i) /^/ 477.603k i/s - 1.404M times in 2.939186s (2.09μs/i) lines 2.485M i/s - 7.028M times in 2.828757s (402.47ns/i) Comparison: lines: 2484631.6 i/s /^/: 477603.3 i/s - 5.20x slower /(?<=\n)/: 422346.5 i/s - 5.88x slower ``` --- test/reline/test_config.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test/reline/test_config.rb b/test/reline/test_config.rb index c00c4f8236..1de8ecaf71 100644 --- a/test/reline/test_config.rb +++ b/test/reline/test_config.rb @@ -21,7 +21,7 @@ class Reline::Config::Test < Reline::TestCase end def test_read_lines - @config.read_lines(<<~LINES.split(/^/)) + @config.read_lines(<<~LINES.lines) set bell-style on LINES @@ -71,7 +71,7 @@ class Reline::Config::Test < Reline::TestCase set bell-style on PARTIAL_LINES end - @config.read_lines(<<~LINES.split(/^/)) + @config.read_lines(<<~LINES.lines) $include included_partial LINES @@ -79,7 +79,7 @@ class Reline::Config::Test < Reline::TestCase end def test_if - @config.read_lines(<<~LINES.split(/^/)) + @config.read_lines(<<~LINES.lines) $if Ruby set bell-style audible $else @@ -91,7 +91,7 @@ class Reline::Config::Test < Reline::TestCase end def test_if_with_false - @config.read_lines(<<~LINES.split(/^/)) + @config.read_lines(<<~LINES.lines) $if Python set bell-style audible $else @@ -104,7 +104,7 @@ class Reline::Config::Test < Reline::TestCase def test_if_with_indent %w[Ruby Reline].each do |cond| - @config.read_lines(<<~LINES.split(/^/)) + @config.read_lines(<<~LINES.lines) set bell-style none $if #{cond} set bell-style audible @@ -119,7 +119,7 @@ class Reline::Config::Test < Reline::TestCase def test_unclosed_if e = assert_raise(Reline::Config::InvalidInputrc) do - @config.read_lines(<<~LINES.split(/(?<=\n)/), "INPUTRC") + @config.read_lines(<<~LINES.lines, "INPUTRC") $if Ruby LINES end @@ -128,7 +128,7 @@ class Reline::Config::Test < Reline::TestCase def test_unmatched_else e = assert_raise(Reline::Config::InvalidInputrc) do - @config.read_lines(<<~LINES.split(/(?<=\n)/), "INPUTRC") + @config.read_lines(<<~LINES.lines, "INPUTRC") $else LINES end @@ -137,7 +137,7 @@ class Reline::Config::Test < Reline::TestCase def test_unmatched_endif e = assert_raise(Reline::Config::InvalidInputrc) do - @config.read_lines(<<~LINES.split(/(?<=\n)/), "INPUTRC") + @config.read_lines(<<~LINES.lines, "INPUTRC") $endif LINES end @@ -146,7 +146,7 @@ class Reline::Config::Test < Reline::TestCase def test_default_key_bindings @config.add_default_key_binding('abcd'.bytes, 'EFGH'.bytes) - @config.read_lines(<<~'LINES'.split(/^/)) + @config.read_lines(<<~'LINES'.lines) "abcd": "ABCD" "ijkl": "IJKL" LINES