Skip to content

Instantly share code, notes, and snippets.

@senny
Created February 26, 2014 13:44
Show Gist options
  • Save senny/9229689 to your computer and use it in GitHub Desktop.
Save senny/9229689 to your computer and use it in GitHub Desktop.
require 'benchmark'
def extract_schema_and_table(name)
table, schema = name.scan(/[^".\s]+|"[^"]*"/)[0..1].collect{|m| m.gsub(/(^"|"$)/,'') }.reverse
[schema, table]
end
def extract_pg_identifier_from_name(name)
match_data = name.start_with?('"') ? name.match(/\"([^\"]+)\"/) : name.match(/([^\.]+)/)
if match_data
rest = name[match_data[0].length, name.length]
rest = rest[1, rest.length] if rest.start_with? "."
[match_data[1], (rest.length > 0 ? rest : nil)]
end
end
n = 50_000
Benchmark.bm(7) do |x|
x.report("connection.extract_pg_identifier_from_name:") {
n.times do
extract_pg_identifier_from_name(%(my_schema.my_table))
extract_pg_identifier_from_name(%(my_schema."my_table"))
extract_pg_identifier_from_name(%("my_schema".my_table))
extract_pg_identifier_from_name(%("my_schema"."my_table"))
extract_pg_identifier_from_name(%(my_table))
extract_pg_identifier_from_name(%("my_table"))
end
}
x.report("Utils.extract_schema_and_table:") {
n.times do
extract_schema_and_table(%(my_schema.my_table))
extract_schema_and_table(%(my_schema."my_table"))
extract_schema_and_table(%("my_schema".my_table))
extract_schema_and_table(%("my_schema"."my_table"))
extract_schema_and_table(%(my_table))
extract_schema_and_table(%("my_table"))
end
}
end
activerecord :: (master*) » ruby benchmark.rb ~/Projects/rails/activerecord
user system total real
connection.extract_pg_identifier_from_name: 1.080000 0.010000 1.090000 ( 1.077225)
Utils.extract_schema_and_table: 2.470000 0.000000 2.470000 ( 2.483858)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment