Speaking of PHP voodoo

My original mapping code over at Indianablogs was taking anywhere from 1.5 seconds to 3 seconds to go from City Name to a completely dynamic map with a tiny dot on it. This was really not sitting well with me because my profiling of the script showed that 99.8% of the time was being consumed by retrieving some data from the web and scraping it for geographical coordinates.

I also implemented some database caching that caches database results in the session so there only need be 3 database queries each session and that data need not be parsed into a structured array every page load (that would take up another 2-3 seconds.

So anyways, last night I made a script that would scrape geographical coordinates for as many of the cities in Indiana and store them in the database. Because they are now being pulled from the database I have that data cached like everything else in the session and my script times are SO much better. I was able to decrease the script time anywhere from 100-500%.

Profiling Infomation:

  • City Name and ID from cityTree: 2.69ms – 3.18%
  • Retreive City from cityInfoCache: 0.22ms – 0.26%
  • Convert coords to pixels: 0.12ms – 0.14%
  • Produce Map: 81.5ms – 96.42%

Now the only bottle neck is actually drawing the picture. In reality it isn’t that big of a bottle neck when you assume that drawing multiple objects in php doesn’t increase the script time linearly.

Maybe you’re asking yourself why decreasing a script’s run time from 3 seconds to 180 milliseconds is so important. Well in the future I’ll be plotting over a hundred points on this mother and I don’t need each run taking 3 seconds. That’d mean that just to view the indiana map would make a user have to wait at least 300 seconds. Not good at all.

Theoretically, with the current system we should be able to generate and view an image with 100 points plotted on it in about 18 seconds which is still too long for me. I’m going to have to play around with different file formats, or cache the results of plotting and only plot new sites on the image.

Edit : I ended up making a finely tuned plotting function, pieced out some part s of other functions and improved the caching system a bit and I am now able to plot all 456 cities in indiana that I have geographical coordinates for in anywhere from 118 to 130 milliseconds. This means I can plot roughly 3800 points per second.