Wydajność funkcji

2022-02-08 23:02:54
Autor: Grzesiek Tarka

PHP 7.4.27

1. Test

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1000000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $data[] = sprintf($format, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}
1 2 3 4 5 6 7 8 9 10
265ms 246ms 242ms 251ms 208ms 190ms 273ms 205ms 213ms 197ms

średnia: 229 ms

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1000000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $data[] = \sprintf($format, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}

 

1 2 3 4 5 6 7 8 9 0
157ms 250ms 229ms 209ms 184ms 207ms 171ms 177ms 176ms 153ms

średnia: = 191,3 ms

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1000000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $data[] = \sprintf($format, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}

 

1 2 3 4 5 6 7 8 9 10
177ms 273ms 228ms 200ms 223ms 186ms 242ms 201ms 188ms 190ms

średnia: = 210,3 ms

1 2 3
229 ms 191,3 ms 210,3 ms

2. Test

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1500000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $format2 = 'There are %s monkeys in the %s test %s';
            $format3 = 'There are %s monkeys in the %s test %s';
            $data[] = sprintf($format, $num, $location, $i);
            $data[] = sprintf($format2, $num, $location, $i);
            $data[] = sprintf($format3, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}
1 2 3 4 5 6 7 8 9 10
1204ms 1248ms 1087ms 978ms 906ms 953ms 1391ms 975ms 949ms 1228ms

średnia: 1091,9 ms

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use function sprintf;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1500000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $format2 = 'There are %s monkeys in the %s test %s';
            $format3 = 'There are %s monkeys in the %s test %s';
            $data[] = sprintf($format, $num, $location, $i);
            $data[] = sprintf($format2, $num, $location, $i);
            $data[] = sprintf($format3, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}
1 2 3 4 5 6 7 8 9 10
954ms 1038ms 1083ms 910ms 830ms 919ms 957ms 874ms 900ms 892ms

średnia: 935,7 ms

3. Test

<?php

namespace Enp\Bundle\Admin\WebBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class DashboardController extends Controller
{
    public function dashboardAction()
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 1500000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $format2 = 'There are %s monkeys in the %s test %s';
            $format3 = 'There are %s monkeys in the %s test %s';
            $data[] = \sprintf($format, $num, $location, $i);
            $data[] = \sprintf($format2, $num, $location, $i);
            $data[] = \sprintf($format3, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}
1 2 3 4 5 6 7 8 9 10
899 944 869 894 914 861 769 760 857 1189

średnia: 895,6

1 2 3
1091,9 ms 935,7 895,6
<?php

declare(strict_types=1);

namespace App\Controller\Front\Blog;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
    #[Route('/blog', name: 'blog', priority: 1)]
    public function index(Request $request): Response
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 5000000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $data[] = sprintf($format, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}

 

//bez niczego
129.917 seconds

//wersja z use function
115.557 seconds

wersja z back slashem
116.523 seconds

4. Test

<?php

declare(strict_types=1);

namespace App\Controller\Front\Blog;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class BlogController extends AbstractController
{
    #[Route('/blog', name: 'blog', priority: 1)]
    public function index(Request $request): Response
    {
        $timeStart = microtime(true);

        $data = [];

        for ($i = 1; $i <= 100000; $i++) {
            $num = 5;
            $location = 'tree';
            $format = 'There are %s monkeys in the %s test %s';
            $format2 = 'There are %s monkeys in the %s test %s';
            $format3 = 'There are %s monkeys in the %s test %s';
            $data[] = sprintf($format, $num, $location, $i);
            $data[] = sprintf($format2, $num, $location, $i);
            $data[] = sprintf($format3, $num, $location, $i);
        }
        echo count($data);
        $endtime = microtime(true);
        $duration = $endtime - $timeStart;
        $milliseconds = round($duration * 1000);
        echo '<h3><b>czas</b> ' . $milliseconds . ' milliseconds</h3>';
        die;
    }
}

90.595 seconds
82.123 seconds
80.918 seconds

ab -n 1000 -c 1 http://localhost/blog-symfony/public/blog

kategorie
Komentarze wyłączone