Why does building Symfony Response take so much time, use so much memory, and what can I do about it?

Test script:

#!/usr/bin/php
<?php
require __DIR__.'/../www/autoload.php';

$start = microtime(true);
$mem = memory_get_usage(true);
$resp = new \Symfony\Component\HttpFoundation\Response();
$elapsed = (microtime(true)-$start)*1000;
$used_mem = memory_get_usage(true)-$mem;
echo number_format($elapsed,2)." ms\n";
echo number_format($used_mem/1024,1)." KiB\n";

Conclusion:

2.25 ms
256.0 KiB

The funny thing is, if I put it in a loop, the cost did not increase:

$resp = [];
for($i=0; $i<100; ++$i) {
    $resp[] = new \Symfony\Component\HttpFoundation\Response();
}

6.73 ms
512.0 KiB

But I need only one answer, so that doesn't really matter.

Looking at the constructor for Response, he does nothing. It simply initializes several variables.

2ms is a significant part of my response time, I would really like it to be possible.

+4
source share
1 answer

, XDebug: http://www.xdebug.org/docs/profiler

, "", , .

"" - 206, \DateTime, \DateTimeZone.

, , , :

<?php

require 'vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/Response.php';
require 'vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/HeaderBag.php';
require 'vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/ResponseHeaderBag.php';

$start = microtime(true);
$mem = memory_get_usage(true);
$resp = new \Symfony\Component\HttpFoundation\Response();
$elapsed = (microtime(true)-$start)*1000;
$used_mem = memory_get_usage(true)-$mem;
echo number_format($elapsed,2)." ms\n";
echo number_format($used_mem/1024,1)." KiB\n";

206

3.57 ms
0.0 KiB

206

0.32 ms
0.0 KiB
+5

All Articles