Skip to content

Instantly share code, notes, and snippets.

View raggi's full-sized avatar

James Tucker raggi

View GitHub Profile
require "benchmark"
@callbacks = %w(
TimerFired
ConnectionData
ConnectionUnbound
ConnectionAccepted
ConnectionCompleted
LoopbreakSignalled
ConnectionNotifyReadable
@raggi
raggi / benches.rb
Created July 16, 2009 17:18
bigdecimal char_round
require "benchmark"
require 'bigdecimal'
class BigDecimal
# raggi after hint from apeiros (round 1)
def raggi_round(n = 15)
s = to_s('f')[0,n+1]
i = s.index('.')
dp = n - i
dp = 0 if dp < 0
@raggi
raggi / scope_challenge.rb
Created July 29, 2009 11:43
Should this work? Do you know what it's doing?
# The Question: Should this work?
# The Bonus Question: Does it, and in what capacity?
def parse_message(str)
last = nil
@parser ||= Parser.new { |message| last = message }
@parser << str
last
end
require 'benchmark'
def many
10000
end
def push(left_array_size, right_array_size)
left = Array.new(left_array_size) { |n| n }
right = Array.new(right_array_size) { |n| n }
Benchmark.realtime{ many.times{ |i| left.push(*right) } }
require "benchmark"
STRING = '1234567890'
FROZEN = STRING.dup.freeze
FROZEN_KEY = {
FROZEN => :something
}
require "benchmark"
KEY = '1234567890'
HASH = {
KEY => KEY
}
TESTS = 2_000_000
def times_for_period(date)
regular_hours_by_week = Hash.new(0)
regular_hours = 0
other_hours = Hash.new(0)
work_days_for_period(date).each do |d|
times = d.times
regular_hours += times[:regular_hours]
@raggi
raggi / quicktalk.rb
Created November 12, 2009 00:43
A quick hack to post messages to talker chat rooms
#!/usr/bin/env ruby
domain, room, *message = ARGV
token = File.read(ENV['HOME'] + '/.talker').strip
abort <<-TEXT unless domain && room && token
Usage: #{File.basename($0)} domain room_id [message]
Reads from stdin if no message supplied
Expects a user token in $HOME/.talker
TEXT
@raggi
raggi / sync_io_bug.rb
Created December 2, 2009 18:15
ruby sync io bug on fbsd + osx, 1.8
# This bug seems to occur on 1.8.6..1.9 on OSX and FreeBSD. It's known not to occur on Linux.
require 'rubygems'
require 'minitest/unit'
include MiniTest::Assertions
require 'tempfile'
path = nil
begin
@raggi
raggi / inject_concat_vs_map.rb
Created December 3, 2009 12:17
closures aren't cheap, srsly
require "benchmark"
arr = [1,2,3,4,5,6,7,8,9,0] * 100
GC.start
TESTS = 10_000
Benchmark.bmbm do |results|
results.report("map:") { TESTS.times { arr.map { |i| i } } }
results.report("inj:") { TESTS.times { a = []; arr.inject(a, :<<) } }
end