File: speed_read_line.rb

package info (click to toggle)
ruby-tty-reader 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 804 kB
  • sloc: ruby: 1,759; sh: 4; makefile: 4
file content (48 lines) | stat: -rw-r--r-- 1,203 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

require "stringio"
require "benchmark/ips"

require_relative "../lib/tty-reader"

input = StringIO.new("abc\n")
output = StringIO.new
$stdin = input
reader = TTY::Reader.new(input: input, output: output)

Benchmark.ips do |x|
  x.report("gets") do
    input.rewind
    $stdin.gets
  end

  x.report("read_line") do
    input.rewind
    reader.read_line
  end

  x.compare!
end

# v0.9.0
#
# Calculating -------------------------------------
#                 gets      2.181M (± 3.2%) i/s -     10.988M in   5.043046s
#            read_line      1.265k (± 2.8%) i/s -      6.324k in   5.005288s
#
# Comparison:
#                 gets:  2181160.4 i/s
#            read_line:     1264.6 i/s - 1724.74x  slower
#
# v0.1.0
#
# Calculating -------------------------------------
#                 gets     51729 i/100ms
#            read_line       164 i/100ms
# -------------------------------------------------
#                 gets  1955255.2 (±3.7%) i/s -    9776781 in   5.008004s
#            read_line     1215.1 (±33.1%) i/s -       5248 in   5.066569s
#
# Comparison:
#                 gets:  1955255.2 i/s
#            read_line:     1215.1 i/s - 1609.19x slower