Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Haversine
(version: 0)
Comparing performance of:
Distance1 vs Distance2 vs Distance3 vs Distance4 vs Distance5
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var TO_RAD = Math.PI / 180; function toRad(deg) { return deg * TO_RAD; } var coords = new Array(); function randomLat() { return Math.random() * 180 - 90; } function randomLong() { return Math.random() * 360 - 180; } for (var i = 0; i < 5000; i++) { coords.push([randomLat(), randomLong(), randomLat(), randomLong()]); } function compute(coord, f) { return f(coord[0], coord[1], coord[2], coord[3]); } function test(f) { for (var i = 0; i < coords.length; i++) { var coord = coords[i]; compute(coord, f); } } var R = 6371009; // m var R2 = 6371009 * 2; // m function distance1(lat1, lon1, lat2, lon2) { var R = 6371009; // m var dLat = toRad(lat2 - lat1); var dLon = toRad(lon2 - lon1); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); return R * c; } function distance2(lat1, lon1, lat2, lon2) { var aLat = toRad(lat1); var bLat = toRad(lat2); var dLat2 = (bLat - aLat) / 2; var dLon2 = toRad(lon2 - lon1) / 2; var x = Math.sin(dLat2) * Math.sin(dLat2) + Math.cos(aLat) * Math.cos(bLat) * Math.sin(dLon2) * Math.sin(dLon2); return R * 2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x)); } function distance3(lat1, lon1, lat2, lon2) { var aLat = toRad(lat1); var bLat = toRad(lat2); var dLat2 = (bLat - aLat) * 0.5; var dLon2 = toRad(lon2 - lon1) * 0.5; var sindLat = Math.sin(dLat2); var sindLon = Math.sin(dLon2); var x = sindLat * sindLat + Math.cos(aLat) * Math.cos(bLat) * sindLon * sindLon; return R2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x)); } function distance4(lat1, lon1, lat2, lon2) { var aLat = lat1 * TO_RAD; var bLat = lat2 * TO_RAD; var dLat2 = (bLat - aLat) * 0.5; var dLon2 = (lon2 - lon1) * TO_RAD * 0.5; var sindLat = Math.sin(dLat2); var sindLon = Math.sin(dLon2); var x = sindLat * sindLat + Math.cos(aLat) * Math.cos(bLat) * sindLon * sindLon; return R2 * Math.atan2(Math.sqrt(x), Math.sqrt(1 - x)); } function distance5(lat1, lon1, lat2, lon2) { var aLat = toRad(lat1); var bLat = toRad(lat2); var dLat2 = (bLat - aLat) * 0.5; var dLon2 = toRad(lon2 - lon1) * 0.5; var sindLat = Math.sin(dLat2); var sindLon = Math.sin(dLon2); var x = sindLat * sindLat + Math.cos(aLat) * Math.cos(bLat) * sindLon * sindLon; return R2 * Math.asin(Math.sqrt(x)); }
Tests:
Distance1
test(distance1)
Distance2
test(distance2)
Distance3
test(distance3)
Distance4
test(distance4)
Distance5
test(distance5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Distance1
Distance2
Distance3
Distance4
Distance5
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 131 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Distance1
19532.8 Ops/sec
Distance2
2646.7 Ops/sec
Distance3
2682.3 Ops/sec
Distance4
2660.3 Ops/sec
Distance5
3012.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark definition is represented by the JSON object that contains the JavaScript code for the test case. In this case, we have five test cases: `distance1`, `distance2`, `distance3`, `distance4`, and `distance5`. Each test case corresponds to a different implementation of calculating the distance between two points on a sphere (the Earth). **Test Cases** Each test case is defined by a single line of code that calls the `test()` function, passing the name of the distance calculation function as an argument. For example, the first test case `distance1` is defined by the line `test(distance1)`. **Distance Calculation Functions** The five distance calculation functions are implemented using trigonometric formulas to calculate the distance between two points on a sphere. Each function takes four arguments: `lat1`, `lon1`, `lat2`, and `lon2`, which represent the latitude and longitude of the two points. Here's a brief overview of each function: * `distance1`: uses the Haversine formula to calculate the distance between two points. * `distance2`: uses a simplified version of the Haversine formula, but with a different implementation detail (explained later). * `distance3`, `distance4`, and `distance5`: use similar implementations as `distance2`, but with minor variations. **Implementation Differences** The main difference between `distance2` and the other three functions is that it uses a faster algorithm to calculate the sine of the latitude and longitude values. Specifically, it uses a lookup table to precompute the sine values for common latitude and longitude ranges. This optimization reduces the number of trigonometric operations required by the algorithm. **Test Results** The latest benchmark results show the performance of each test case on a Chrome 120 browser running on a Windows desktop. The results are displayed as an array of objects, each containing information about the execution frequency (ExecutionsPerSecond) and other metadata. Overall, this benchmark is designed to compare the performance of different distance calculation algorithms implemented in JavaScript, with a focus on optimization and accuracy.
Related benchmarks:
Haversine
Geo distance
Testando
Haversine performance
Comments
Confirm delete:
Do you really want to delete benchmark?