Skip to content

Instantly share code, notes, and snippets.

Created January 28, 2010 18:13
Show Gist options
  • Save anonymous/288980 to your computer and use it in GitHub Desktop.
Save anonymous/288980 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <sys/time.h>
#include <stdio.h>
double clock() {
struct timeval tv;
gettimeofday(&tv,NULL);
return tv.tv_sec + 1e-6*tv.tv_usec;
}
static inline double sqr(double x) {return x*x;}
static double radians(double deg) {return deg * M_PI / 180;}
static double distance(double latA,double lngA,double latB,double lngB) {
double
radius = 6371,
latAr = radians(latA),
lngAr = radians(lngA),
latBr = radians(latB),
lngBr = radians(lngB),
deltaLat = latBr - latAr,
deltaLng = lngBr - lngAr;
return radius * 2 * asin(sqrt(sqr(sin(deltaLat/2))
+ cos(latAr) * cos(latBr) * sqr(sin(deltaLng/2))));
}
int main(void) {
int i,j,k,l;
double ii,jj,kk,ll,start,total = 0;
start = clock();
for (i=0,ii=-90; i<73; i++,ii+=2.5)
for (j=0,jj=-180; j<145; j++,jj+=2.5)
for (k=0,kk=-90; k<73; k++,kk+=2.5)
for (l=0,ll=-180; l<145; l++,ll+=2.5)
total += distance(ii,jj,kk,ll);
printf("total = %g, time = %g\n",total,clock()-start);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment