I honestly can't figure out why this script takes a long time to execute to begin with.. I am testing out something similar (but simplified a bit i'm sure). And i'm getting results that are extremely fast and i'm not even running a that fast of a computer.. What i did was i used the script that i posted on here a few days ago, i ran that and put several hundres thousand entries in my db.. Now i'm testing how long it takes to insert a new unique id into this table. My results are typically 0.0012 seconds. The worst i've had was like 0.0025 seconds. I know your page is going to have a lot of other things to slow it down as well, but this obviously isnt one of the things that should be slowing it down. Maybe the problem lies somewhere else....
here is the script i wrote so you can test it
PHP Code:
<?php
function getRandomAlphaNumID() {
$randomAlphabet = null;
$randomNumber = null;
for($i=1; $i<=4; $i++) {
$randomNumber .= rand(0,9);
$randomAlphabet .= chr(rand(65,90));
}
return $randomAlphabet.$randomNumber;
}
function start_time() {
$starttime = microtime();
$startarray = explode(" ", $starttime);
return $startarray[1] + $startarray[0];
}
function end_time($starttime) {
$endtime = microtime();
$endarray = explode(" ", $endtime);
$endtime = $endarray[1] + $endarray[0];
return round($endtime - $starttime, 5);
}
?>
<html>
<head><title>Random Test</title>
<body>
<?php
if (isset($_POST['submit'])) {
$starttime= start_time();
$con = mysql_connect("localhost", "myusername", "mypasswd");
mysql_select_db("random");
do {
$num = getRandomAlphaNumID();
$check = mysql_query("SELECT COUNT(*) FROM test WHERE random_id='$num'");
} while (mysql_result($check, 0,0)>0);
$insert = mysql_query("INSERT INTO test (random_id) values('$num')");
if (!$insert) {
echo mysql_error();
}
$endtime = end_time($starttime);
echo "that took $endtime seconds to execute <br />";
}
?>
<p>Press enter to generate a new random id and test how long it takes<p>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="submit" value="generate"/>
</form>
</body>
</html>
Bookmarks