#!/usr/bin/env perl use strict; use warnings; use Benchmark qw(cmpthese timethese); use Geo::Distance::XS; use GIS::Distance; # When benchmarking, need to have it call import/unimport before the # code is executed. my $orig_timethis_sub = \&Benchmark::timethis; { no warnings 'redefine'; *Benchmark::timethis = sub { my $sub = ('perl' eq $_[2] ? 'un' : '') . 'import'; Geo::Distance::XS->$sub(); $orig_timethis_sub->(@_); }; } # Lon, Lat my @coord = (-76.668851, 39.179689, -118.408618, 33.943532); my $geo = Geo::Distance->new; my $gis = GIS::Distance->new; my %gis_formula = ( hsin => 'Haversine', polar => 'Polar', cos => 'Cosine', gcd => 'GreatCircle', mt => 'MathTrig', tv => 'Vincenty', ); sub geo { my $d = $geo->distance(mile => @coord); } sub gis { my $d = $gis->distance(@coord[ 1, 0, 3, 2 ]); return $d->mile; } for my $formula (qw(hsin polar cos gcd mt tv)) { print "---- [ Formula: $formula ] ------------------------------------\n"; $geo->formula($formula); $gis->formula($gis_formula{$formula}); Geo::Distance::XS->unimport; printf "perl - distance from BWI to LAX: %s miles\n", geo(); Geo::Distance::XS->import; printf "xs - distance from BWI to LAX: %s miles\n", geo(); printf "gis::fast - distance from BWI to LAX: %s miles\n", gis(); print "\n"; my $benchmarks = timethese - 1, { perl => \&geo, xs => \&geo, 'gis::fast' => \&gis, }; cmpthese $benchmarks; print "\n"; }