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
def transform_hash(original, options={}, &block) | |
options[:safe_descent] ||= {} | |
new_hash = {} | |
options[:safe_descent][original.object_id] = new_hash | |
original.inject(new_hash) { |result, (key,value)| | |
if (options[:deep] && Hash === value) | |
value = options[:safe_descent].fetch( value.object_id ) { | |
transform_hash(value, options, &block) | |
} | |
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
# Do you ever define #method_missing and forget #respond_to? I sure | |
# do. It would be nice if we could do them both at the same time. | |
module MatchMethodMacros | |
def match_method(matcher, &method_body) | |
mod = Module.new do | |
define_method(:method_missing) do |method_name, *args, &blk| | |
if matcher === method_name.to_s | |
instance_exec(method_name, *args, blk, &method_body) | |
else |
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 'rspec' | |
T = 10000 | |
N = 200 | |
class ConcurrentSender | |
def initialize(collaborator,message,threads,sends) | |
@collaborator,@message,@threads,@sends = collaborator,message,threads,sends | |
@tasks = [] | |
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
#!/usr/bin/env ruby | |
# benchmarks for http://stackoverflow.com/questions/11349544/ruby-optimize-the-comparison-of-two-arrays-with-duplicates/11352055#comment14951595_11352055 | |
require 'rubygems' | |
require 'multiset' | |
require 'benchmark' | |
small_b = "cheddaar".split(//) | |
small_a = "cheddar".split(//) |
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
#!/usr/bin/env ruby | |
# Benchmark answers to http://stackoverflow.com/questions/13002594/handling-array-of-hashes | |
require 'benchmark' | |
A = [{"lib1"=>"30"}, {"lib2"=>"30"}, {"lib9"=>"31"}, {"lib2"=>"31"}, {"lib3"=>"31"}, {"lib1"=>"32"}, {"l\ | |
ib2"=>"32"}, {"lib1"=>"33"}, {"lib3"=>"36"}, {"lib2"=>"36"}, {"lib1"=>"37"}] | |
def to_a_flat | |
A.map(&:to_a).flatten(1).each_with_object({}) do |(k, v), h| |
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'set' | |
N = 10_000_000 | |
Benchmark.bm do |x| | |
1.upto(10) do |i| | |
a = (0...i).to_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
#!/usr/bin/env ruby | |
require 'benchmark' | |
require 'forwardable' | |
puts RUBY_DESCRIPTION | |
class TestMe | |
extend Forwardable | |
def_delegators :@arr, :each |
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
# 456976 four character words | |
STR = ('aaaa'..'zzzz').to_a * ' ' | |
N = 10 | |
puts RUBY_DESCRIPTION | |
puts "#{N} times on string of size #{STR.size}" |
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
#!/usr/bin/env ruby | |
require 'set' | |
require 'benchmark' | |
N = 5 | |
HSZ = 150_000 | |
ASZ = 25_000 |
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
#!/usr/bin/env ruby | |
require 'benchmark' | |
N = 1_000_000 | |
A = (0..10).to_a | |
def map_plain | |
A.map {|e| e.to_s} |
OlderNewer