Skip to content

Calculate geo distance with lon lat coordinates using Vincenty's formula with fast C++ python module

License

Notifications You must be signed in to change notification settings

dariusarnold/vincentys-formula

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vincentys-formula

Speed comparison between Python (Numpy and pure Python), C and C++.

This repository contains Vincenty's formula in

  • C, to be used with the ctypes Python module.
  • C++, compiled with pybind11
  • Python (Numpy and pure Python)

The formula calculates the azimuth and distance between two points on the surface of a spheroid. For more information about the formula see: https://en.wikipedia.org/wiki/Vincenty's_formulae

Compile C code to a shared library

gcc -O3 -shared -lm -Wl,-soname,vinc -o vinc.so -fPIC main.c

Compile c++ code to an extension module

c++ -O3 -Wall -shared -std=c++11 -fPIC `python3 -m pybind11 --includes` vinc.cpp -o vinc_cpp`python3-config --extension-suffix` -I /usr/include/python3.6

Generate test results

Execute the vinc_c_vs_python.py file

On a Xeon E3-1246v3 @ 3.50GHz

Testing function vinc
NumPy best average out of 1000 runs:  9.9E-05 secs
Python best average out of 1000 runs: 9.7E-06 secs
C best average out of 1000 runs:      1.1E-06 secs
C++ best average out of 1000 runs:    5.8E-07 secs

Numpy results:  559580.5062148898 -3.0210528410061204
Python results: 559580.5062148898 -3.0210528410061204
C results:      559567.3915137552 -3.021053212988765
C++ results:    559580.5061678728 -3.0210528410061204

Conclusion

Numpy is slower than pure Python since only scalars are used during the calculation. C takes 1.1 µs per function call, C++ 0.6 µs and python 10 µs. Binding the extension with pybind11 requires less boilerplate code than ctypes, and is not less efficient. The generated module is larger though (122 kB in C++ vs 16 kB for C).

About

Calculate geo distance with lon lat coordinates using Vincenty's formula with fast C++ python module

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published