-
-
Save nakajima/16967 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Running with tiny string | |
- only scanning then injecting: 0.0131118297576904 seconds | |
- scanning groups and injecting: 0.0120480060577393 seconds | |
- scanning then splat hashing: 0.0159380435943604 seconds | |
- splitting then splat hashing: 0.00326204299926758 seconds | |
Running with medium string | |
- only scanning then injecting: 0.13988208770752 seconds | |
- scanning groups and injecting: 0.12324595451355 seconds | |
- scanning then splat hashing: 0.242349147796631 seconds | |
- splitting then splat hashing: 0.0289840698242188 seconds | |
Running with ridiculous string | |
- only scanning then injecting: 1.17678284645081 seconds | |
- scanning groups and injecting: 1.26292014122009 seconds | |
- scanning then splat hashing: 18.0726480484009 seconds | |
- splitting then splat hashing: 0.281298160552979 seconds | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Hash[*options.split(/&|=/)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'benchmark' | |
require 'paramer' | |
class String | |
def pad_to(size) | |
spaces = size - length | |
return self unless spaces > 0 | |
spaces.times { self << " " } | |
self | |
end | |
end | |
def bench(name, options={}) | |
$stdout.print "#{name} ".pad_to(33) | |
$stdout.flush | |
result = Benchmark.measure { yield } | |
puts "#{result.real} seconds" | |
end | |
def run(name, string) | |
puts name | |
bench("- only scanning then injecting:") { string.scan_then_inject } | |
bench("- scanning groups and injecting:") { string.scan_groups_then_inject } | |
bench("- scanning then splat hashing:") { string.scan_then_splat_hash } | |
bench("- splitting then splat hashing:") { string.split_then_splat_hash } | |
puts | |
end | |
TINY = ("foo=bar&this=that&klass=eigen=_method=get&" * 500).freeze | |
MED = (TINY * 10).freeze | |
LONG = (MED * 10).freeze | |
run "Running with tiny string", TINY | |
run "Running with medium string", MED | |
run "Running with ridiculous string", LONG |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe String, "param parsing" do | |
before(:each) do | |
@result = { "foo" => "bar", "this" => "that" } | |
end | |
it "scans then injects" do | |
"foo=bar&this=that&".scan_then_inject.should == @result | |
end | |
it "scans groups then injects" do | |
"foo=bar&this=that&".scan_groups_then_inject.should == @result | |
end | |
it "scans splat hashes" do | |
"foo=bar&this=that&".scan_then_splat_hash.should == @result | |
end | |
it "splits then splat hashes" do | |
"foo=bar&this=that&".split_then_splat_hash.should == @result | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class String | |
def scan_then_inject | |
scan(/\w+=\w+/).inject({}) { |h,opt| kv = opt.split('='); h[kv.first] = kv.last; h } | |
end | |
def scan_groups_then_inject | |
scan(/\w+=\w+/).inject({}) { |h,opt| kv = opt.split('='); h[kv.first] = kv.last; h } | |
end | |
def scan_then_splat_hash | |
Hash[*scan(/(\w+)=(\w+)/).flatten] | |
end | |
def split_then_splat_hash | |
Hash[*split(/&|=/)] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment