Skip to content

Instantly share code, notes, and snippets.

@tristil
Forked from fixlr/sort_benchmark.rb
Last active December 20, 2015 13:09
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 tristil/6136694 to your computer and use it in GitHub Desktop.
Save tristil/6136694 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'natural_sort'
require 'naturally'
require 'natcmp'
require './lib/sort_authority'
require './lib/sort_authority/ext/enumerable'
require './sensible_sort'
def randomish_string
(0...20).map do
if rand(2) == 0
(('a'..'z').to_a + [" ", "_"])[rand(28)]
else
rand(10)
end
end.join
end
ary = 100_000.times.map {|i| randomish_string }
Benchmark.bm do |bm|
bm.report('sort ') { ary.sort }
bm.report('strnatcmp.c ') { ary.natural_sort }
bm.report('naturalsort gem') { NaturalSort::naturalsort(ary) }
bm.report('naturally gem ') { Naturally.sort(ary) }
bm.report('natcmp gem ') { ary.sort {|a,b| Natcmp.natcmp(a, b) } }
bm.report('sensible_sort ') { ary.sensible_sort }
end
# user system total real
# sort 0.110000 0.000000 0.110000 (0.112114)
# strnatcmp.c 0.560000 0.000000 0.560000 (0.560879)
# naturalsort gem 161.590000 1.200000 162.790000 (162.883611)
# naturally gem 7.940000 0.020000 7.960000 (7.965125)
# natcmp gem 51.150000 0.190000 51.340000 (51.364688)
# sensible_sort 21.230000 0.160000 21.390000 (21.406591)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment