Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
First of all, what everyone else has said about first trimming your data set applies: for a given longitude and latitude, you should be able to come up with boundaries that are definitely in your set and boundaries that are definitely out. That being said, you can speed up your comparison even in the C code by doing this: I assume that in your main loop you do something like:
while (read_from_file(&newzip, &newlat, &newlon)) { mydist = great_circle_distance(newlat,newlon,origlat,origlon); if (mydist < 25) { // do stuff to add it to file } if (mydist < 50) { ... } ... }
Instead, replace it with this:
float dist1cosine = cos(25.0 / EARTH_RADIUS); float dist2cosine = cos(50.0 / EARTH_RADIUS); float dist3cosine = cos(75.0 / EARTH_RADIUS); while (read_from_file(&newzip, &newlat, &newlon)) { mydistcos = great_circle_distance_cosine(newlat,newlon, origlat,origlon); if (mydistcos >= dist1cosine) { // do stuff to add it to file } if (mydistcos >= dist2cosine) { ... } ... }
where you've got:
static inline float great_circle_distance_cosine(float lat1, float long1, float lat2, float long2) { float delta_long, temp1, temp2, delta_lat; delta_lat = lat2 - lat1; delta_long = long2 - long1; temp1 = sin(lat1) * sin(lat2); temp2 = cos(delta_lat); temp2 -= temp1; temp2 *= cos(delta_long); /* result is cos(lat1) cos(lat2) [ cos(lon1 - lon2) ] + sin(lat1) sin(lat2) */ return (temp1 + temp2); }
The point of this exercise is to avoid the atan2 and sqrt calls, which are done to take the arccos of sqrt(temp) in the original code. Over the range you're dealing with, arccos is strictly decreasing, (hence the switch from "<" to ">=" in the comparison) which means you don't need to take arccos every time in order to Actually, I think that you could translate this algorithm into perl and, even without applying any pre-distance filtering, get better results than your current program.

Edit by tye, remove PRE tags surrounding CODE tags

In reply to Re: Zipcode Proximity script by Anonymous Monk
in thread Zipcode Proximity script by hacker

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2021-02-28 01:11 GMT
    Find Nodes?
      Voting Booth?

      No recent polls found