Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created May 14, 2019 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoshCheek/f8415943f5fbec2ec27e9e379d443331 to your computer and use it in GitHub Desktop.
Save JoshCheek/f8415943f5fbec2ec27e9e379d443331 to your computer and use it in GitHub Desktop.
Progression of "Terminal Drawer"
require 'benchmark'
RUBY_VERSION # => "2.6.0"
n = 10_000
# arbitrarily sized callstack
def call_at_depth(n, &b)
n.zero? ? b.() : call_at_depth(n-1,&b)
end
ary = []
Benchmark.bmbm do |bm|
bm.report 'throw/catch' do
n.times { raise "msg" rescue $!.message }
end
bm.report 'raise/rescue, no backtrace access' do
n.times { call_at_depth(100) { raise "msg" } rescue $!.message }
end
bm.report 'create hash' do
n.times { ary << {"abc" => "def", "ghi" => "jkl"} }
end
end
# >> Rehearsal ---------------------------------------------------------------------
# >> throw/catch 0.013765 0.000290 0.014055 ( 0.014170)
# >> raise/rescue, no backtrace access 0.113554 0.003640 0.117194 ( 0.118470)
# >> create hash 0.008786 0.000700 0.009486 ( 0.009505)
# >> ------------------------------------------------------------ total: 0.140735sec
# >>
# >> user system total real
# >> throw/catch 0.011952 0.000524 0.012476 ( 0.012508)
# >> raise/rescue, no backtrace access 0.110460 0.006539 0.116999 ( 0.117850)
# >> create hash 0.004555 0.000363 0.004918 ( 0.004981)
# 50 over
ruby -rio/console -e '
[6,3].each{|n|$><<"\e[?100#{n}h";at_exit{$><<"\e[?100#{n}l"}}
kb=IO. open 0
c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+8.times. map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
s=kb.raw{kb.readpartial 99}
case s
when ?\C-c;break
when /^\d$/;c=s. to_i
when /^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#{$3};#{$2}H\e[4#{c}m "
end}'
# 40 over
ruby -rio/console -e '
[6,3].each{|n|$><<"\e[?100#{n}h";at_exit{$><<"\e[?100#{n}l"}}
K=IO. new 0
c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+8.times. map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
case s=K.raw{K.readpartial 99}
when ?\C-c;break
when /^\d$/;c=s. to_i
when /^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#$3;#$2H\e[4#{c}m "
end}'
# 33 over
ruby -rio/console -e '
[6,3].each{|n|$><<"\e[?100#{n}h";at_exit{$><<"\e[?100#{n}l"}}
K=IO. new 0
c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
case s=K.raw{K.readpartial 99}
when ?\C-c;break
when/^\d$/;c=s
when/^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#$3;#$2H\e[4#{c}m "
end}'
# 28 over
ruby -rio/console -e '
$><<"\e[?1006h\e[?1003h";at_exit{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
case s=K.raw{K.readpartial 99}
when ?\C-c;break
when/^\d$/;c=s
when/^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#$3;#$2H\e[4#{c}m "
end}'
# 22 over
ruby -rio/console -e '
$><<"\e[?1006h\e[?1003h";END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
case s=K.raw{K.readpartial 99}when ?\C-c;exit when/^\d$/;c=s when/^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#$3;#$2H\e[4#{c}m "
end}'
# 19 over
ruby -rio/console -e '$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
case s=K.raw{K.readpartial 99}when ?\C-c;1/0when/^\d$/;c=s when/^\e\[<(0|32);(\d+);(\d+)/;$><<"\e[#$3;#$2H\e[4#{c}m "
end}'
# 9 over
ruby -rio/console -e'$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
s=K.raw{K.readpartial 99}
s==?\C-c&&1/0
s=~/^\d$/?c=s:s=~/^\e\[<(0|32);(\d+);(\d+)/&&$><<"\e[#$3;#$2H\e[4#{c}m "}'
# 5 over (now you have to press "q" to quit instead of C-c)
ruby -rio/console -e'$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
s=K.raw{K.readpartial 99}
s==?q?1/0:s=~/^\d$/?c=s:s=~/^\e\[<(0|32);(\d+);(\d+)/&&$><<"\e[#$3;#$2H\e[4#{c}m "}'
# 4 over (now you have to press "q" to quit instead of C-c)
ruby -rio/console -e'$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
s=K.raw{K.readpartial 99}
s==?q?1/0:s=~/^\d$/?c=s:s=~/^\e\[<(0|32);(\d+);(\d+)/&&$><<"\e[#$3;#$2H\e[4#{c}m "}'
# 3 over
ruby -rio/console -e'$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+[*?0..?7].map{|i|"\e[4#{i}m #{?*if i==c}#{i} "}*""
s=K.raw{K.readpartial 99}
s==?q?1/0:s=~/^\d/?c=s:s=~/\e\[<(0|32);(\d+);(\d+)/&&$><<"\e[#$3;#$2H\e[4#{c}m "}'
# got it!
ruby -rio/console -e'$><<"\e[?1006h\e[?1003h"
END{$><<"\e[?1006l\e[?1003l"}
K=IO. new c=0
$><<"\e[40m\e[2J"
loop{$><<"\e[H"+(?0..?7).map{|i|"\e[4#{i}m#{i==c ??*:" "}#{i} \e[4#{c}m"}*""
s=K.raw{K.readpartial 99}
s==?q?1/0:s=~/^\d/?c=s:s=~/<(0|32);(\d+);(\d+)/&&$><<"\e[#$3;#$2H "}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment