Skip to content

Instantly share code, notes, and snippets.

@fixlr
Created August 2, 2013 00:25
Show Gist options
  • Save fixlr/6136569 to your computer and use it in GitHub Desktop.
Save fixlr/6136569 to your computer and use it in GitHub Desktop.
Benchmarks of natural sorting in Ruby.
require 'benchmark'
require 'natural_sort'
require 'naturally'
require 'natcmp'
require './lib/sort_authority'
require './lib/sort_authority/ext/enumerable'
require './sensible_sort'
ary = ['x 1'] * 100_000
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.000000 0.000000 0.000000 ( 0.001758)
# strnatcmp.c 0.030000 0.000000 0.030000 ( 0.029083)
# naturalsort gem 0.060000 0.000000 0.060000 ( 0.061879)
# naturally gem 0.990000 0.020000 1.010000 ( 1.009634)
# natcmp gem 1.140000 0.010000 1.150000 ( 1.141950)
# sensible_sort 2.280000 0.000000 2.280000 ( 2.286274)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment