Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def sql
/\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/
end
def sql_frozen
/\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/.freeze
end
SQLo = /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/o
SQL = /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/
SQLFrozen = /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/.freeze
require 'rubygems'
require 'benchmark/ips'
STATEMENT = "SELECT * FROM card_payments"
Benchmark.ips do |x|
x.config(:time => 45, :warmup => 2)
x.report "inline /o" do
STATEMENT =~ /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/o
end
x.report "inline" do
STATEMENT =~ /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/
end
x.report "inline [frozen]" do
STATEMENT =~ /\A\s*(SELECT|INSERT|UPDATE|DELETE).*(FROM|JOIN)\s*`?(my_interesting|tables)`?/.freeze
end
x.report "method" do
STATEMENT =~ sql
end
x.report "method [frozen]" do
STATEMENT =~ sql_frozen
end
x.report "constant /o" do
STATEMENT =~ SQLo
end
x.report "constant" do
STATEMENT =~ SQL
end
x.report "constant [frozen]" do
STATEMENT =~ SQLFrozen
end
end
__END__
Calculating -------------------------------------
inline /o 51.435k i/100ms
inline 52.298k i/100ms
inline [frozen] 50.497k i/100ms
method 48.986k i/100ms
method [frozen] 45.558k i/100ms
constant /o 52.396k i/100ms
constant 49.258k i/100ms
constant [frozen] 52.501k i/100ms
-------------------------------------------------
inline /o 815.788k (± 9.8%) i/s - 36.262M
inline 818.869k (± 9.7%) i/s - 36.399M
inline [frozen] 761.042k (±11.6%) i/s - 33.631M
method 791.291k (± 6.4%) i/s - 35.466M
method [frozen] 694.295k (±17.6%) i/s - 29.977M
constant /o 751.494k (±13.6%) i/s - 33.009M
constant 746.568k (±12.2%) i/s - 33.003M
constant [frozen] 757.508k (±12.3%) i/s - 33.443M
@drbrain

This comment has been minimized.

Copy link

@drbrain drbrain commented Feb 10, 2015

You should try the once flag, /…/o also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment