Jump to content

Archived

This topic is now archived and is closed to further replies.

DanielStead

Need help in speeding up this script as it has 45,000 co ordiantes

Recommended Posts

I have a script that basicallydraws around each township on a map and makes each township linkable in total i have about 498 townships and a total of 44,337 co ordiantes. the script is below and basicallys has 2 loops one to make the sections of the area map linkable and one to actually draw the round the townships. the reason for this is that we have four processes to run on each township so we will fill the township with a different colour depending on what processes have been run on the township.

Here is the code

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]<?php
session_start();
require_once("include/check_login.php");
require_once("include/database.php");
require_once("include/functions.php");
?>


<html>
<head>
<title>Cheshire County Council (v0.1) (v0.1)</title>
<script type="text/javascript" src="js/wz_jsgraphics.js"></script>
<link href="css/cheshire.css" rel="stylesheet" type="text/css">
<style type="text/css">
<!--
.style1 {color: #0000CC}
-->
</style>

<body>
<?php
echo " <div class=\"mapcontainer\" style=\"width:1500px\" style=\"height:1069px\">\n";

echo " <img src=\"images/CheshireMap_Final_Rect.jpg\" border=\"0\" usemap=\"ParishMap\" id=\"anotherCanvas\">\n";
echo " <map name=\"ParishMap\">\n";

$sql = "SELECT Distinct Parish, Township FROM CheshirePolygons2";
$result = mysql_query($sql) or die("Unable to select from Polygons1");

while ($row = mysql_fetch_array($result, MYSQL_BOTH))
{
$Parish = $row["Parish"];
$Township = $row["Township"];

$sql2 = "SELECT Parish, Township, x, y FROM CheshirePolygons2 WHERE Parish = '{$Parish}' AND Township = '{$Township}' ORDER BY OrderId";
$result2 = mysql_query($sql2) or die("Unable to select from Polygons2");

$i = 0;

unset ($coordinate);
unset ($coordinate2x);
unset ($coordinate2y);

while ($row2 = mysql_fetch_array($result2, MYSQL_BOTH))
{
$coordinate[$i] = (((($row2['x'] - 312822) / 10) / 7.118) + 4) . ',' . (1069 - (((($row2['y'] - 334713) / 10) / 7.116) - 2));
$coordinate2x[$i] = (((($row2['x'] - 312822) / 10) / 7.118) + 4);
$coordinate2y[$i] = (1069 - (((($row2['y'] - 334713) / 10) / 7.116) - 2));

$name = $row2["Township"];
$parish = $row2["Parish"];

$i++;
}

echo "<script type=\"text/javascript\">\n";
echo "<!--\n";
echo " function myDrawFunction()\n";
echo " {\n";
echo " jg_doc.setColor(\"blue\");\n";
echo " jg_doc.drawPolyline(new Array(" . WriteCoordinatesX($coordinate2x) . "), new Array(" . WriteCoordinatesY($coordinate2y) . "));\n";
echo " jg_doc.paint();\n";
echo " }\n";
echo " var jg_doc = new jsGraphics();\n";
echo " var jg = new jsGraphics(\"anotherCanvas\");\n";
echo " myDrawFunction();\n";
echo " </script>\n";

echo " <area HREF=\"view.php?hotspot=$name\" ALT=\"$parish\" TITLE=\"$name\" SHAPE=\"poly\" onmouseover=\"myDrawFunction($name,$parish)\" COORDS=\"" . WriteCoordinates($coordinate) . "\">\n";
mysql_free_result($result2);
}

mysql_free_result($result);

echo " </map>";

function WriteCoordinates($coordinates)
{
$return = "";
for($i = 0; $i < sizeof($coordinates); $i++)
{
$return .= $coordinates[$i];

if($i != (sizeof($coordinates) - 1))
{
$return .= ",";
}
}
return $return;
}

function WriteCoordinatesX($coordinate2x)
{
$return = "";
for($i = 0; $i < sizeof($coordinate2x); $i++)
{
$return .= $coordinate2x[$i] + 5;

if($i != (sizeof($coordinate2x) - 1))
{
$return .= ",";
}
}
return $return;
}

function WriteCoordinatesY($coordinate2y)
{
$return = "";
for($i = 0; $i < sizeof($coordinate2y); $i++)
{
$return .= $coordinate2y[$i] + 6;

if($i != (sizeof($coordinate2y) - 1))
{
$return .= ",";
}
}
return $return;
}

echo " <div>\n";
?>
</body>
</html>
<?php

unset($_SESSION['error_msg']);
?>
[/quote]

Share this post


Link to post
Share on other sites
first thing you can do to make it faster is to change your for loops.

In the declaration of the loop you use sizeof() as the count limit. This is very slow as it is evaluate each time the loop runs.

change it to

$stop = sizeof($cordinates);
for($i = 0; $i < $stop; $i++) {.....

do similar for the other loop.

Share this post


Link to post
Share on other sites
Use constants not numbers - it's quicker to calculate when a numerical value is pre-assigned to a program 'constant'. For example, changing all the absolute values in a line such as this:
[code]$coordinate[$i] = (((($row2['x'] - 312822) / 10) / 7.118) + 4) . ',' . (1069 - (((($row2['y'] - 334713) / 10) / 7.116) - 2));[/code]
should improve execution speed.

Share this post


Link to post
Share on other sites
Have you tried using php and creating a GD image then displaying that?

I don't know if it would be faster - just curious if you have benchmarked the two methods.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.