Jump to content


Photo

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


  • Please log in to reply
3 replies to this topic

#1 DanielStead

DanielStead
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 03 March 2006 - 03:55 PM

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]

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 03 March 2006 - 04:07 PM

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.

follow me on twitter @PHPsycho

#3 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 03 March 2006 - 04:19 PM

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:
$coordinate[$i] = (((($row2['x'] - 312822) / 10) / 7.118) + 4) . ',' . (1069 - (((($row2['y'] - 334713) / 10) / 7.116) - 2));
should improve execution speed.
Legend has it that reading the manual never killed anyone.
My site

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 03 March 2006 - 08:23 PM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users