M
M
mifa2011-08-09 13:33:13
PHP
mifa, 2011-08-09 13:33:13

PHP: echo is very slow

A strange problem, the simplest code:

echo $content;

Where $content = 100-110 kb of pure html - it takes almost a second!
I tried to create a separate page, where there are not even variables, just echo and 100 kb of text, it's still very slow.

In Google, such complaints are found, but all are dated 2005-2006 and the solutions come down to playing with output_buffering or splitting $content into parts, which does not help.

Dear Habrarazum, help, is echo really so slow, or is there something to fix in the conservatory?

Answer the question

In order to leave comments, you need to log in

12 answer(s)
M
Maxim, 2011-08-09
@maxout

There was such a problem in production. What is the OS on the server?

R
rPman, 2011-08-09
@rPman

Almost certainly a problem with the webserver or maybe DNS?
gentoo, apache, slow old AMD Sempron(tm) Processor 3400+
a.php: ps but if you have the same thing with cli php then you need to sound the alarm!!! pps I had a moment at the first vds when the simplest lib_curl (more precisely, curl_... calls from php) mercilessly suspended the virtual machine at 100% for seconds, but at the same time, the curl / wget console utility worked without problems ... while I was generally figuring out where the brakes came from , the problem resolved itself (it lasted for hours), so I wrote down openvz (or rather virtuozzo) to the account of potential glitches and made a notch in memory that this is also possible. <?php echo str_repeat('0123456789012345',8192); ?>

# date; curl http://localhost/a.php >/dev/null;date
Втр Авг 9 20:15:03 NOVST 2011
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 128k 0 128k 0 0 31.4M 0 --:--:-- --:--:-- --:--:-- 41.6M
Втр Авг 9 20:15:03 NOVST 2011

R
Renat Ibragimov, 2011-08-09
@MpaK999

ADF: The problem is localized, and it's not in PHP: any webserver gives a static file of 100 kb for about a second. Both ngix and apache. Both have almost zero load.

G
Gleb Starkov, 2011-08-09
@colonel

Is it executed starting from the moment of the request from the browser?
There is still a lot to work out before echo, starting with the web server.
You need to check everything, calculate exactly where the delay is.

A
AlexeyK, 2011-08-09
@AlexeyK

What do you mean by "done"?

T
theRavel, 2011-08-09
@theRavel

How do you measure execution time? Are you running through the console or through the browser?

V
Vitali Borovik, 2011-08-09
@WAYS

I don't know how you measure, try this.
<?PHP
$start = microtime();
// Твое ехо
$end = microtime();
echo 'Время - ' . ($endtime - $starttime) . ' микросекунд';
?>

S
s0rr0w, 2011-08-09
@s0rr0w

Tell us more about the server on which you are testing all this. Interested in his hardware, operating system, current load.

E
Eugene, 2011-08-09
@Nc_Soft

ob_start() doesn't help?
What is output buffering in php.ini?

G
galaxy, 2011-08-09
@galaxy

APD2: If you run the script from the console, it takes 2.2 seconds (one big echo in the script). And if you redirect the output to /dev/null, and microtime to error_log, then it takes 0.003 seconds

It's a network (most likely an ssh channel between you and the server). Example:
> time cat [text log at 1.3M]
real 0m52.905s
user 0m0.000s
sys 0m0.012s
> time cat [same file] > log.txt
real 0m0.015s
user 0m0.000s
sys
0m0.004s web server, time can be spent on resolving (as indicated above), routing/nat/switching in the LAN itself, connection + plus TCP for some time (more precisely, several round-trip packets) is needed to reach full speed.
Weed out the reasons one by one: nslookup or downloading directly over IP (DNS), ping (packet speed over local, packet loss), tcpdump is a universal tool, we look at what is given when and how.

A
AxisPod, 2011-08-09
@AxisPod

Considering the fact that it slows down, you should check the disk system and the Internet channel, it makes sense to execute locally ab. In general, I would advise starting with strace, it will show the execution time of system calls well, there can be many problems.

P
p4s8x, 2011-08-10
@p4s8x

I'm not entirely sure if you've read this: The echo function in PHP can take more than 1 second to complete .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question