Skip to content

Instantly share code, notes, and snippets.

@gettalong
Created June 2, 2012 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gettalong/2856929 to your computer and use it in GitHub Desktop.
Save gettalong/2856929 to your computer and use it in GitHub Desktop.
Some small benchmarks used during the creation of kramdown
# -*- coding: utf-8 -*-
require 'benchmark'
class Test
CONST = 5
N = 1_000_000
def test_const
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { CONST }
end
results.report "two" do
N.times { self.class::CONST}
end
end
end
def test_string_insert
Benchmark.bm 20 do |results|
x = 'my'
results.report 'one' do
N.times { s = "data"; s.insert(0, x) }
end
results.report "two" do
N.times { s = "data"; s = "#{x}#{s}" }
end
end
end
# one/two simliar, three slower
def test_string_concat
Benchmark.bm 20 do |results|
x = 'my' * 100
results.report 'one' do
N.times { "data#{x}" }
end
results.report "two" do
N.times { "data" << x }
end
results.report 'three' do
N.times { "data" + x }
end
end
end
# similar performance
def test_assignment
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { x = {}; x[5] = 6; x[7] = 8 }
end
results.report "two" do
N.times { (x ||= {})[5] = 6; (x ||= {})[7] = 8 }
end
end
end
# getch about 35% faster
def test_strscan_getch
require 'strscan'
y = StringScanner.new("hallöchen")
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { y.reset; 8.times {y.scan(/./)} }
end
results.report "two" do
N.times { y.reset; 8.times {y.getch} }
end
end
end
# rest about 55% (47%) faster (with setting position to eos)
def test_strscan_rest
require 'strscan'
y = StringScanner.new("hallöchen\nsome\nother data")
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { y.pos = 4; y.scan(/.*/m) }
end
results.report "two" do
N.times { y.pos = 4; y.rest; y.terminate }
end
end
end
INDENT = /^(?:\t| {4})/
# two about 60% faster
def test_string_gsub
y = " this is some string\n with some data\n that continues here"
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { y.gsub(/\n( {0,3}\S)/, ' \\1').gsub!(INDENT, '') }
end
results.report "two" do
N.times { x = y; x.gsub!(/\n( {0,3}\S)/, ' \\1'); x.gsub!(INDENT, '') }
end
end
end
# two about 35% faster (using ! methods and using normal methods)
def test_string_gsub_vs_tr
y = "123. hallo ich habe! <span>hiere</span>"
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { x = y.dup; x.gsub!(/^[^a-zA-Z]+|[^a-zA-Z0-9 -]+/, ''); x.gsub!(' ', '-') }
end
results.report "two" do
N.times { x = y.dup; x.gsub!(/^[^a-zA-Z]+/, ''); x.tr!('^a-zA-Z0-9 -', ''); x.tr!(' ', '-')}
end
end
end
def invoke_me
end
# two about 50% faster (using either hash line)
def test_invoke_method
me = :me
hash = Hash.new {|h,k| h[k] = "invoke_#{k}"}
#hash = {:me => "invoke_me"}
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { send("invoke_#{me}") }
end
results.report "two" do
N.times { send(hash[me]) }
end
end
end
# similar runtimes
def test_string_times
Benchmark.bm 20 do |results|
results.report 'one' do
N.times { indent = 2; indent += 2; ' '*indent }
end
results.report "two" do
N.times { indent = ' '; indent += indent; indent }
end
end
end
# runtimes are equal
def test_string_concat2
Benchmark.bm 20 do |results|
x = 'my'
results.report 'one' do
N.times { "data#{x}>#{x}\n" }
end
results.report "two" do
N.times { "data" << x << '>' << x << "\n" }
end
end
end
# simliar runtimes
def test_regexp_match
Benchmark.bm 20 do |results|
x = "my some thing is\nasdfasd fsad fasdf adsf \ndsaf adfadsfsa\n"
results.report 'one' do
N.times { x =~ /\n\Z/ }
end
results.report "two" do
N.times { x[-1,1] == "\n" }
end
end
end
# two 50% faster
def test_equal_vs_regexp_match
Benchmark.bm 20 do |results|
x = "script"
results.report 'one' do
N.times { x =~ /^script$/ }
end
results.report "two" do
N.times { x == 'script' }
end
end
end
# two 50% faster
def test_equal_vs_regexp_match2
Benchmark.bm 20 do |results|
x = "script"
re = /^script$/
results.report 'one' do
N.times { x =~ re }
end
results.report "two" do
N.times { x == 'script' }
end
end
end
attr_accessor :accessor_test_1
def accessor_test_2
@accessor_test_2
end
# one 33% faster on Ruby 1.8.7, 6% on 1.9.2
def test_accessors
@accessor_test_1 = 1
@accessor_test_2 = 1
Benchmark.bm do |results|
results.report 'one' do
N.times { accessor_test_1 }
end
results.report "two" do
N.times { accessor_test_2 }
end
end
end
attr_accessor :assign_test_1
def assign_test_3
@assign_test_1 ||= []
end
def assign_test_2
@assign_test_2 ||= []
#only useful when done on few objects on methods that are accessed often
class << self; attr_accessor :assign_test_2; end
end
# one 40% faster when two=assign_test_3, equal when two=assign_test_2
def test_accessors
@assign_test_1 = []
Benchmark.bm do |results|
results.report 'one' do
N.times { assign_test_1 }
end
results.report "two" do
N.times { assign_test_2 }
end
end
end
# ary.select {|d| }.length vs ary.count
def test_select_length_vs_count
ary = []
1.upto(1000) {|i| ary << rand(100)}
Benchmark.bm do |results|
results.report 'one' do
N.times { ary.count {|c| c > 50} }
end
results.report "two" do
N.times { ary.select {|c| c > 50}.length }
end
end
end
#
def test_unpack_vs_ord
string = "abcd".force_encoding('ASCII-8BIT')
x = nil
Benchmark.bm do |results|
results.report 'one' do
N.times { x = string.unpack('C4'); (x[0] << 21) + (x[1] << 14) + (x[2] << 7) + x[3] }
end
results.report "two" do
N.times { (string[0].ord << 21) + (string[1].ord << 14) + (string[2].ord << 7) + string[3].ord }
end
end
end
def test_camelize
s = "my_underscore"
Benchmark.bm do |results|
re1 = /^(\w)|_(\w)/
results.report 'one' do
N.times { x = s.dup; x.gsub!(re1) { ($1 || $2).upcase } }
end
results.report "two" do
N.times { x = s.dup; x.split('_').inject(''){ |s,x| s << x[0..0].upcase + x[1..-1] } }
end
re2 = /_/
results.report "three" do
N.times { x = s.dup; x.split(re2).inject(''){ |s,x| s << x[0..0].upcase + x[1..-1] } }
end
end
end
# two about 20% faster on 1.8.7, 8% faster on 1.9.3
def test_regexp_creation
Benchmark.bm 20 do |results|
x = "script"
re = /^script$/
results.report 'one' do
N.times { x =~ re }
end
results.report "two" do
N.times { x =~ /^script$/ }
end
end
end
end
# Change this to run a benchmark
Test.new.test_regexp_creation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment