Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created March 22, 2019 10:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save JoshCheek/20a7d1b5eda3fbf7e99a1707f42348b3 to your computer and use it in GitHub Desktop.
Ruby with --jit

https://twitter.com/_ianks/status/1108950820354031618

Version

$ ruby -v
ruby 2.6.0preview2 (2018-05-31 trunk 63539) +JIT [x86_64-darwin17]

With JIT

$ time ruby --jit -e w,h=$COLUMNS/2-1,$LINES'
walkers = []
10.times do
 walkers << [
   "\e[4#{rand(7)+1}m",
   [[rand(w),rand(h)]]*10
 ]
end
show = lambda { |(x,y)| "\e[#{y+1};#{2*x+1}H  " }
1000.times do
 print "\e[0m" + walkers.map { |_color, points| show[points.pop] }.join
 walkers.each do |color, points|
   x, y = points[0]
   x = (x+rand(3)-1) % w
   y = (y+rand(3)-1) % h
   points.unshift [x, y]
   print color + points.map(&show).join
 end
 sleep 0.01
end
puts "\e[0m"
'
       12.72 real        11.64 user         1.98 sys

Without JIT

$ time ruby -e w,h=$COLUMNS/2-1,$LINES'
walkers = []
10.times do
  walkers << [
    "\e[4#{rand(7)+1}m",
    [[rand(w),rand(h)]]*10
  ]
end
show = lambda { |(x,y)| "\e[#{y+1};#{2*x+1}H  " }
1000.times do
  print "\e[0m" + walkers.map { |_color, points| show[points.pop] }.join
  walkers.each do |color, points|
    x, y = points[0]
    x = (x+rand(3)-1) % w
    y = (y+rand(3)-1) % h
    points.unshift [x, y]
    print color + points.map(&show).join
  end
  sleep 0.01
end
puts "\e[0m"
'
       12.27 real         0.56 user         0.14 sys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment