Suggestions to improve execution time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Suggestions to improve execution time

Post by dull1554 »

my code:

Code: Select all

$time_start = microtime(true);
$port = "80";
$address = "big.oscar.aol.com";
$sn = $_GET['sn'];
$status="unknown";


$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if ($socket < 0) {
    echo "error, reason: " . socket_strerror($socket) . "\n";
}

$result = socket_connect($socket, $address, $port);
if ($result < 0) {
    echo "error, reason: ($result) " . socket_strerror($result) . "\n";
}

$in = "GET /" . $sn . "?on_url=true&off_url=false HTTP/1.0\r\n";
$in .= "Connection: Close\r\n\r\n";
$out = '';

socket_write($socket, $in, strlen($in));

while ($out = socket_read($socket, 2048)) {

if (strstr($out, "true")==True) {
$status="online";
} elseif (strstr($out, "false")==True) {
$status="offline";
} else {
$status="unknown";
}

}

socket_close($socket);

echo $status;
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<br /><br />Executed in $time seconds\n";
my average execution time is around .05-.08 seconds

would this be faster if it used Curl insted of sockets, or how else could this be tweaked to increase speed.

this code will be slightly addapted to update a colum in a large database of AIM users so speed is important as to not tax my server.
Last edited by dull1554 on Thu Jul 06, 2006 1:11 pm, edited 1 time in total.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

oh and the code by the way sends a request to big.oscar.aol.com to determine wither or not a aim user is on or off line
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post by dull1554 »

allright, i guess im glad no one has any suggestions. i'll fly whit what i got.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

i main reason your code is slightly slower than most is the fact you are making an external connection with another machine. That takes time and speeding it up is outside your control.
my average execution time is around .05-.08 seconds
For a code requesting stuff over the net that's already really good.
would this be faster if it used Curl insted of sockets, or how else could this be tweaked to increase speed.
try slower. leave it as it is.
Post Reply