mapado / haversine

Calculate the distance between 2 points on Earth
MIT License
326 stars 62 forks source link

Remove map() call #27

Closed adamchainz closed 4 years ago

adamchainz commented 4 years ago

Avoid the overhead of constructing the tuple, map object, and deconstructing it:

In [1]: from math import radians

In [2]: a = b = c = d = 1.0

In [3]: %timeit ar, br, cr, dr = map(radians, (a, b, c, d))
427 ns ± 1.54 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

In [4]: %timeit ar = radians(a) ; br = radians(b) ; cr = radians(c) ; dr = radians(d)
271 ns ± 1.79 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

Whilst a micro-optimization, haversine() is often called on large data sets so this can lead to an overall large speedup.

adamchainz commented 4 years ago

Also I came across this because some code was passing a None into haversine, and getting a TypeError from radians, but I can't tell which variable from the plain stack trace since they're all converted in this line.

jdeniau commented 4 years ago

Thanks for the improvement !

jdeniau commented 4 years ago

Released in 2.2.0