Skip to content

Instantly share code, notes, and snippets.

View jrunning's full-sized avatar

Jordan Running jrunning

View GitHub Profile
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
@jrunning
jrunning / 01_sym_vs_str.rb
Last active August 16, 2016 14:57
Is Ruby hash lookup faster with symbol keys than string keys?
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
@jrunning
jrunning / Cm9I-0.rb
Created August 12, 2016 19:02
https://repl.it/Cm9I/0 created by jrunning
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! %
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...
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"
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
@jrunning
jrunning / states_regexp.rb
Created May 12, 2016 22:44
A regular expression that matches every U.S. state or territory's name or postal abbreviation, more or less
expr = /^(
a(
[klsz] |
(
l(abam|ask)? |
rizon
)a |
merican\ssamoa |
r(kansas)?
) |
@jrunning
jrunning / states_regexp.rb
Created May 12, 2016 22:21
A regular expression that matches every US state or territory's name or postal abbreviation (I think)?
expr = /^(
a(
[ksz] |
(
l(abam|ask)? |
rizon
)a |
merican\ssamoa |
r(kansas)?
) |
@jrunning
jrunning / 01_regexp_vs_regexp_trie.rb
Created March 24, 2016 19:07
Regexp.union vs. RegexpTrie.union
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
@jrunning
jrunning / 01_mustache_liquid_benchmark.rb
Last active March 27, 2022 23:09 — forked from anonymous/01_mustache_liquid_benchmark.rb
Mustache vs. Liquid templates benchmark (naïve)
# 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"