This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark/ips" | |
NUMBERS = (0..100).to_a.shuffle | |
STRINGS = ("abcd".."efgh").to_a.shuffle | |
def val | |
case rand(0..1) | |
when 0 then NUMBERS.sample | |
else STRINGS.sample | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark/ips" | |
require "securerandom" | |
sym_hash = {} | |
str_hash = {} | |
1000.times do |i| | |
# Generate a random key between 3 and 32 characters long | |
SecureRandom.hex[0,rand(3..32)].tap do |key| | |
str_hash[key] = i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn= | |
->s{w=s=~/\n/ | |
i=0 | |
s.scan(/(..|.\n)(?=.{#{w-1}}(..|.\n))?/m).map{|a|(("%4s"%(a*"")).tr($/," ").bytes.reduce(:+)/4.0).round.chr*2}.each_slice(w/2+w%2){|l|puts l*="",l if i%2<1 | |
i+=1}} | |
fn[<<END] | |
%%%%%%%%%%%%% | |
% Hello, % | |
% world! % |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "barby/barcode/code_128" | |
require "barby/outputter/png_outputter" | |
require "benchmark/ips" | |
require "tempfile" | |
dir = Dir.tmpdir | |
Benchmark.ips do |x| | |
x.report { | |
barcode = Barby::Code128B.new(rand(1000000000001..1000000001000)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This... | |
if x == y and y == z | |
puts "This triangle is equilateral" | |
else | |
if x == y or y == z or x == z | |
puts "This triangle is isoceles" | |
else | |
if x != y and y != z and x != z | |
puts "this triangle is scalene" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "benchmark/ips" | |
require "csv" | |
# Data: Fielding.csv from Lahman Baseball Database, sorted | |
# http://seanlahman.com/baseball-archive/statistics/ | |
FILENAME = File.expand_path("Fielding-sorted.csv", __dir__) | |
MATCH_FIELD = "kruegot01" # Record in the middle of the file | |
NUM_ROWS = 5 # Grab this many rows (for lack of anything better to do) | |
def method_a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expr = /^( | |
a( | |
[klsz] | | |
( | |
l(abam|ask)? | | |
rizon | |
)a | | |
merican\ssamoa | | |
r(kansas)? | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
expr = /^( | |
a( | |
[ksz] | | |
( | |
l(abam|ask)? | | |
rizon | |
)a | | |
merican\ssamoa | | |
r(kansas)? | |
) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark/ips' | |
require 'regexp_trie' | |
NUM_WORDS = 1000 # Approximate number of words to be chosen from the dictionary | |
DICT_PATH = './google-10000-english.txt' # https://github.com/first20hours/google-10000-english | |
TEXT_PATH = './big.txt' # http://norvig.com/big.txt (~6MB) | |
num_words_in_dict = `wc -l "#{DICT_PATH}"`.to_i | |
choose_probability = NUM_WORDS.to_f / num_words_in_dict |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a very basic benchmark of Mustache (1.0.2) vs. | |
# Liquid (3.0.6) rendering. It does two sets of benchmarks: One with | |
# "precompiled" templates (i.e. the Mustache::Template or | |
# Liquid::Template object is kept between runs) and one without (a new | |
# Template object is instantiated for each run). This benchmark tests | |
# a basic loop; no other features e.g. partials or conditionals | |
# are tested. | |
require "benchmark/ips" | |
require "mustache" |