Skip to content

Instantly share code, notes, and snippets.

@aasmith
Last active January 23, 2020 00:13
Show Gist options
  • Save aasmith/9cb36b6a9114ab9b0e8901cfa5df52c7 to your computer and use it in GitHub Desktop.
Save aasmith/9cb36b6a9114ab9b0e8901cfa5df52c7 to your computer and use it in GitHub Desktop.
Ruby URI Benchmark

To run the benchmarks (MacOS):

brew install icu4c
bundle config build.uri_parser -- "--with-cppflags=-I/usr/local/opt/icu4c/include" "--with-ldflags=-L/usr/local/opt/icu4c/lib" "--with-cxxflags=-std=c++11"

(no need to use brew link; it won't work anyway...)

bundle
bundle exec ruby bench.rb 
require "benchmark/ips"
require "uri"
require "addressable"
require "uri_parser"
URI_ASCII = "http://example.com/path/to/resource/"
URI_INTL = "http://руцентр.рф/Iñtërnâtiônàlizætiøn!?i18n=true"
Benchmark.ips do |x|
x.report("URI") { URI.parse(URI_ASCII) }
x.report("Addressable") { Addressable::URI.parse(URI_ASCII) }
x.report("uri_parser") { URIParser.new(URI_ASCII) }
# doesn't work
# x.report("URI Intl") { URI.parse(URI_INTL) }
x.report("Addressable Intl") { Addressable::URI.parse(URI_INTL) }
x.report("uri_parser Intl") { URIParser.new(URI_INTL) }
x.compare!
end
# frozen_string_literal: true
source "https://rubygems.org"
gem "addressable"
gem "uri_parser", github: "aasmith/uri_parser", branch: "master"
gem "benchmark-ips"
$ bundle exec ruby bench.rb
Warming up --------------------------------------
URI 18.622k i/100ms
Addressable 6.465k i/100ms
uri_parser 22.454k i/100ms
Addressable Intl 5.385k i/100ms
uri_parser Intl 12.766k i/100ms
Calculating -------------------------------------
URI 193.270k (± 8.1%) i/s - 968.344k in 5.054300s
Addressable 62.432k (± 3.8%) i/s - 316.785k in 5.082109s
uri_parser 238.980k (± 2.8%) i/s - 1.213M in 5.077765s
Addressable Intl 55.547k (± 3.7%) i/s - 280.020k in 5.048548s
uri_parser Intl 135.370k (± 2.9%) i/s - 676.598k in 5.002692s
Comparison:
uri_parser: 238980.3 i/s
URI: 193269.9 i/s - 1.24x slower
uri_parser Intl: 135370.3 i/s - 1.77x slower
Addressable: 62432.2 i/s - 3.83x slower
Addressable Intl: 55546.6 i/s - 4.30x slower
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment