Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AlexYa's avatar

How to speed up PHP?

Hi Guys Need your help again =)

I'm reading algorithms and trying to implement them comparing speed for some languages. And here's a problem: I'm taking 100,000 random nums file and sort them. Java takes 30 second to sort, js - 15 sec and php - 980 sec. wtf ?! what i'm doing wrong ?

Using - PHP 7.0.11

Here is code:

Sorting.php

<?php
namespace Sorting;

abstract class Sorting
{
    protected $data = [];
    public function __construct(array $arr)
    {
        $this->data = $arr;
    }
    abstract public function sort();
    protected function less(int $a, int $b)
    {
        return $a < $b;
    }

    protected function exch(array $arr,int $i,int $j) : array
    {
        $temp = $arr[$i];
        $arr[$i] = $arr[$j];
        $arr[$j] = $temp;

        return $arr;
    }

    public function show()
    {
        $count = count($this->data);
        for ($i = 0 ; $i < $count; $i++) {
            echo $this->data[$i];
        }

        echo PHP_EOL;
    }
}

Selection.php

<?php
namespace Sorting;

class Selection extends Sorting
{
    public function sort()
    {
        $count = count($this->data);
        for ($i = 0; $i < $count; $i++) {
            $min = $i;
            for ($j = $i+1; $j < $count; $j++) {
                if ($this->less($this->data[$j], $this->data[$min])) {
                    $min = $j;
                }
            }
            $this->data = $this->exch($this->data, $i, $min);
        }
    }
}

index.php

<?php
ini_set('memory_limit', '4096M');
set_time_limit(-1);
require __DIR__ . '/../vendor/autoload.php';

use \Sorting\Selection;

$arr = file($argv[1]);
$arr = array_map(function ($item) {
    return (int) trim($item);
}, $arr);

$time = -microtime(true);

$sort = new Selection($arr);
$sort->sort();
$sort->show();

$time += microtime(true);

echo  sprintf('Total execution time: %f s', $time) . PHP_EOL;
0 likes
6 replies
ohffs's avatar

It looks like you're trying to implement 'Enterprise Sortbuzz' rather than an algorithm ;-)

I think you're enterprise-wrapping this :

$numbers = [];
foreach (range(1, 100000) as $i) {
    $numbers[] = rand(1, 100000);
}

$time = -microtime(true);

usort($numbers, function ($a, $b) {
    return $a < $b;
});

$time += microtime(true);

printf("Total execution time: %f s\n", $time);

Which on my machine takes about 0.4s - ymmv :-)

To flesh things out a bit more - Java is compiled so will run the code faster almost no matter what you do for something like this. I would suspect Java compilers are also smart enough these days to replace things like your call to $this->less() just with the inline $a < $b too.

In comparison, PHP is dynamic so every time you call a function, create a new object etc it's got to do a bit more work than Java as it doesn't know 'ahead of time' what it's going to be doing.

JS is obscenely optimised for a lot of common tasks (have a look at the numbers on the julia-lang website for instance). You've got the likes of google, microsoft, mozilla, apple etc all spending huge $$ on making it fast.

If you are after raw speed when trying to implement an basic algorithm wrapped up in classes, method calls that re-imagine existing language features etc - then PHP will look bad compared to lots of other languages (especially compiled ones). On the other hand, if you're writing a realistic application then it'll hold it's own.

2 likes
jlrdw's avatar

Use compiled c classes

1 like
jlrdw's avatar

@jimmck the link you gave above, norton internet security shows as a malicious site. I never visited it, because norton stopped me from going there. Can anyone confirm bad or good for that site?

AlexYa's avatar

I'm not trying to make php the faster one. But I thougth php result will approximately 60-120 sec. I was thinking that i've missed something.....maybe some php config....

If remove all OOP the result is about 360sec. In 3 times faster. Thanks for your comments

Please or to participate in this conversation.