Skip to content

Instantly share code, notes, and snippets.

def recur(n)
if n == 0
bench
else
recur(n-1)
end
end
def bench
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
An exception occurred running at_exit handlers
no block given (LocalJumpError)
Backtrace:
ERROR: the VM is exiting improperly running at_exit handlers
intended operation: :exception
associated value: nil
destination scope: unknown
An exception occurred
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
class H < Hash
def [](key)
super if key.kind_of?(Symbol)
end
@thedarkone
thedarkone / gist:3668383
Created September 7, 2012 18:27
This is not thread safe even on MRI
require 'thread'
bignum = (2 ** (0.size * 8))
start = 0
h = {:a => start}
h[:a] += bignum # get into the Bignum territory
iterations = 100_000
@thedarkone
thedarkone / gist:3645105
Created September 5, 2012 21:27
Ruby volatile APIs
# using accessors:
attr_volatile :table, :size_control
def check_for_resize
while (current_table = table) && MAX_CAP > (table_size = current_table.size) && RESIZING != (size_ctrl = size_control) && size_ctrl < @counter.sum
try_in_resize_lock(current_table, size_ctrl) do
self.table = rebuild(current_table)
(table_size << 1) - (table_size >> 1) # 75% load factor
end
Rehearsal ----------------------------------------------------------
via_case(nil) 0.260000 0.000000 0.260000 ( 0.296114)
via_literal(nil) 0.530000 0.000000 0.530000 ( 0.552677)
via_case("null") 0.750000 0.000000 0.750000 ( 0.761376)
via_literal("null") 0.570000 0.000000 0.570000 ( 0.646138)
via_case("nomatch") 1.610000 0.000000 1.610000 ( 1.669481)
via_literal("nomatch") 0.390000 0.010000 0.400000 ( 0.430214)
------------------------------------------------- total: 4.120000sec
user system total real
module Trigo
[:sin, :cos, :tan].each do |trigo_fun|
class_eval <<-RUBY_EVAL, __FILE__, __LINE__
def #{trigo_fun}(angle)
_trigo(:#{trigo_fun}, angle)
end
RUBY_EVAL
end
private
## Original
def reverse_sql_order(order_query)
order_query.join(', ').split(',').collect { |s|
if s.match(/\s(asc|ASC)$/)
s.gsub(/\s(asc|ASC)$/, ' DESC')
elsif s.match(/\s(desc|DESC)$/)
s.gsub(/\s(desc|DESC)$/, ' ASC')
else
s + ' DESC'
end
h = {}
v = 1
threads = Array.new(my_cpu_count = 4) do
Thread.new do
2_000_000.times {|i| h[i] = v}
end
end
threads.map(&:join)