Created
February 28, 2014 14:18
-
-
Save gglanzani/9271842 to your computer and use it in GitHub Desktop.
Haversine performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "metadata": { | |
| "name": "" | |
| }, | |
| "nbformat": 3, | |
| "nbformat_minor": 0, | |
| "worksheets": [ | |
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "from numpy import sin, cos, pi, sqrt, arcsin, random" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 9 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def get_distance(lat, lon, pcode_lat, pcode_lon):\n", | |
| " \"\"\"\n", | |
| " Find the distance between `(lat,lon)` and the reference point\n", | |
| " `(pcode_lat,pcode_lon)`.\n", | |
| " \"\"\"\n", | |
| " RAD_FACTOR = pi / 180.0 # degrees to radians for trig functions\n", | |
| " lat_in_rad = lat * RAD_FACTOR\n", | |
| " lon_in_rad = lon * RAD_FACTOR\n", | |
| " pcode_lat_in_rad = pcode_lat * RAD_FACTOR\n", | |
| " pcode_lon_in_rad = pcode_lon * RAD_FACTOR\n", | |
| " delta_lon = lon_in_rad - pcode_lon_in_rad\n", | |
| " delta_lat = lat_in_rad - pcode_lat_in_rad\n", | |
| " # Next two lines is the Haversine formula\n", | |
| " inverse_angle = (sin(delta_lat / 2) ** 2 + cos(pcode_lat_in_rad) *\n", | |
| " cos(lat_in_rad) * sin(delta_lon / 2) ** 2)\n", | |
| " haversine_angle = 2 * asin(sqrt(inverse_angle))\n", | |
| " EARTH_RADIUS = 6367 # kilometers\n", | |
| " return haversine_angle * EARTH_RADIUS\n" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 10 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "reference = (52.3905927,4.8412508)\n", | |
| "points = random.randn(400000, 2) * 0.01\n", | |
| "points[:, 0] = points[:, 0] + reference[0]\n", | |
| "points[:, 1] = points[:, 1] + reference[1]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 35 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "def iterate_distance():\n", | |
| " d = []\n", | |
| " for p in points:\n", | |
| " d.append(get_distance(p[0], p[1], reference[0], reference[1]))" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [], | |
| "prompt_number": 27 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%timeit iterate_distance()" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 loops, best of 3: 3.62 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 36 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%timeit [get_distance(p[0], p[1], reference[0], reference[1]) for p in points]" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "1 loops, best of 3: 3.58 s per loop\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 37 | |
| }, | |
| { | |
| "cell_type": "code", | |
| "collapsed": false, | |
| "input": [ | |
| "%timeit get_distance(points[:, 0], points[:, 1], reference[0], reference[1])" | |
| ], | |
| "language": "python", | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "stream": "stdout", | |
| "text": [ | |
| "10 loops, best of 3: 22.7 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "prompt_number": 38 | |
| } | |
| ], | |
| "metadata": {} | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment