iMarc | Interactive Media Architects
  • Portfolio
  • Process
  • About
  • Communiqué
  • Contact
  • Support
  • Search

ZIP Code Distances with PHP & PostgreSQL

by Will Bond - March 9, 2007 / 5:36pm

On a recent project we needed to determine the closest locations to a user's ZIP code, a common feature seen on many web sites. The ZIP code database we use has 42,317 unique ZIP codes, so a US-wide search is quite a bit of work. The example I am testing against below isn't a real-world example since we won't have a location in every ZIP code, however I wanted to determine the most efficient method and tweak my code from there.

A quick search on Google will bring you to a good page about the math behind ZIP code distance calculations. It is located at The Math Forum. I took this formula and implemented it four different ways, including with pure PHP, a PL/pgSQL function, a PL/Python function and the geo_distance() C function/operator included with PostgreSQL.

The Code

For each function I entered our ZIP code here at iMarc (01950) and returned all ZIP codes from the database sorted by distance to 01950 descending. Below are the code fragments, installation procedures and SQL statements I used to test each method:

PHP


PL/pgSQL


PL/Python


geo_distance() C function



The Results

Using these four methods, I tested the complete ZIP code database one two different servers. The first is a very low traffic development box with an Pentium 3 800 MHz processor and 512MB of ram. The second is a very high traffic production web server with 2 Dual Core Xeon 2.8 GHz processors and 2 GB of ram. The results were suprising:

Pentium 3
PHP: 2.49979186058 seconds
PL/pgSQL: 4.0787460804 seconds
PL/Python: 10.1163070202seconds
geo_distance(): 2.10908079147 seconds

2 Xeon Dual Core
PHP: 1.35743308067 seconds
PL/pgSQL: 1.43568515778 seconds
PL/Python: 3.46266412735seconds
geo_distance(): 0.722802877426 seconds

As you can see, the geo_distance() C function had the best results, which was of little surprise. The biggest surprise for me was that PHP came in second with the SQL functions being the slowest.

Depending on your application, these results could be tweaked a little to improve performance. If all you care about is the closest ZIP code to the one provided, you could append "LIMIT 1" to the end of the SQL statements to reduce the number of rows selected.

Appendix A: Test Code

Below you will find my exact test code that I used to get the results printed above.

More Articles Get the RSS Feed Post A Comment

4 Comments

by Robert Mohns   #
on March 10, 2007 / 11:30am
Did you compile the geo_distance() C function, then exec() from PHP? or invoke from command line? i'm curious to know how much time of each method is required to set up and run the code, essentially, vs how much time it spends executing.
by Budi Setiawan   #
on March 10, 2007 / 9:01pm
What kind of method or software do you use to get the time result?
You've done a great research and thanks for sharing.
by Will Bond   #
on March 12, 2007 / 10:24am
@ Robert

I included what you requested in the section about the geo_distance() code. Basically you just install a Postgres extension (using gmake) and then tie it into the database by creating a function.

@ Budi

Thanks for your kind words. I have updated the post to include the php page I used to get the time results.
by Paul Naro   #
on July 13, 2007 / 8:38am
Wow. A colleague sent me this link. Excellent article. I haven't had a need for this functionality, and probably would have gone the PHP or C function call from PostgreSQL. Definitely one of those articles you bookmark for future reference!

Comments have been turned off on this blog.
Read something more recent.

Statements and opinions expressed in this blog and any comments made are the private opinions of the respective poster, and, as such, iMarc LLC is neither responsible nor liable for such content.

iMarc

iMarc is a web development company in Newburyport, MA. This is our blog.
View all blogs or learn more about iMarc.

About the Author

Will's Head Will Bond, Senior Technical Architect
I’m involved with pretty much anything computer related here at iMarc, from servers to coding and markup best practices. I’m also the unofficial resident open source advocate.

After work I spend time with my awesome wife and daughter in and around our home in beautiful Newbury, MA.
More blogs by Will

Search Our Blog

Recent Communiqués

  • for lack of nail
  • You're Not Crazy for Smiling at your Plant
  • Book Recommendation: Writing Tools
  • Redundancy: Good or Bad?
  • Making the Switch to Gmail
  • Character Removal
  • Subway Art
  • Using rsync to synchronize iTunes libraries
  • Enough is Enough.
  • Targeted Inspiration
  • Green Benefit, Good Cause
  • Dashes, Underscores, URLs, SEO and Humans
  • Newburyport Web Geek Meetup, Thursday March 20th
  • 01001110 01001001 01001110: The Revolution Continues
  • iMarc's Newest Member

Popular Communiqués

  • You're Not Crazy for Smiling at your Plant
  • How to Make Links
  • Book Recommendation: Writing Tools
  • Using rsync to synchronize iTunes libraries
  • Patrick, From My Perspective
  • Subway Art
  • Character Removal
  • Green Benefit, Good Cause
  • Dashes, Underscores, URLs, SEO and Humans

Recent Comments

  • You're Not Crazy for Smiling at your Plant

    By Elyse Holladay: Watch out - that notebook looks like it'd bite your hand off!

  • for lack of nail

    By Patrick McPhail: Fixed: http://$controller->url.nin

  • for lack of nail

    By Peter R. Wood: Nice to see they're using an MVC paradigm. Too bad they didn't test their email first.

  • You're Not Crazy for Smiling at your Plant

    By Robert Mohns: "You'll note that there is an odd number so that votes can't end up in a tie." Now we…

  • You're Not Crazy for Smiling at your Plant

    By Fred LeBlanc: You'll note that there is an odd number so that votes can't end up in a tie.

RSS

RSS Icon Learn about RSS and get the feed for our blog.

About iMarc

  • We build custom web sites
  • In-house strategy, design, programming, hosting
  • In business since 1997
  • We’re located in Newburyport, MA
  • Call us at (978) 462-8848

© 2008 iMarc LLC, Contact Us

Links

  • Home
  • Portfolio
  • Client Support
  • Log In
  • (icon)RSS

Meet the Team

Jeff's Head Jeff Turcotte, Developer

I program killer robots websites.

Learn More | Meet the Others