FastHaversine
Haversine calculation written in Ruby that supports distances calculations in both miles and kilometers
Installation
Add this line to your application's Gemfile:
gem 'fast_haversine'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fast_haversine
Usage
For kilometers:
distance = FastHaversine.distance_between([lat1, lon1], [lat2, lon2], :km)
For miles:
distance = FastHaversine.distance_between([lat1, lon1], [lat2, lon2], :mi)
Benchmark
During specs, I ran the following benchmark:
1_000_000.times { FastHaversine.distance_between(sf, chi, :mi) }
0.700000 0.000000 0.700000 ( 0.700143)
This is much faster than a common ruby implementation:
1_000_000.times do
RubyHaversine.distance_between(
[37.7833, -122.4167],
[41.8819, -87.6278],
units: :mi
)
end
7.510000 0.320000 7.830000 ( 7.930863)
Contributing
- Fork it ( https://github.com/paulnsorensen/fast_haversine/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request