
Originally Posted by
toggg
Hi asim,
Right! You save the time socket_create() uses.
The only thing left is the pack() and the if statements. I'm not sure if caching the result at the PHP level would be faster than re-invoking pack each time. The if statements could probably be selectively eliminated using eval to build the function on the fly (much like conditional compilation in C):
PHP Code:
$last_time = time() - 100;
$last_pack = 0;
// ...
$socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0);
$delay = $talk || $usl;
while ($max--)
{
# Cache the pack() operation?
$now = time();
if ($now != $last_time)
{
$last_time = $now;
$last_pack = pack('N', $now);
}
if (@socket_sendto($socket, $last_pack, SOCKOUNT_LTIM, 0x100, SOCKOUNT_NAME) == false)
{
echo "Error by send message ($max)\n";
}
if ($delay)
{
if ($talk)
{
echo $max . ' ';
}
if ($usl)
{
usleep($usl);
}
}
}
The 'eval' version looks like:
PHP Code:
$function_body = '
$last_time = time() - 100;
$last_pack = 0;
// ...
$socket = @socket_create(AF_UNIX, SOCK_DGRAM, 0);
while ($max--)
{
# Cache the pack() operation?
$now = time();
if ($now != $last_time)
{
$last_time = $now;
$last_pack = pack("N", $now);
}
if (@socket_sendto($socket, $last_pack, SOCKOUNT_LTIM, 0x100, SOCKOUNT_NAME) == false)
{
echo "Error by send message ($max)\n";
}
';
if ($talk)
{
$function_body .= 'echo $max . " ";
';
}
if ($usl)
{
$function_body .= 'usleep(' . $usl . ');
';
}
$function = create_function('$max', $function_body);
$function($max);
=Austin
Bookmarks