PHP vs Go sorting performance

Published by Dev Kordeš on 08/30/2017

Expanding on my really really simple "benchmarking" thingy that I described a few blog posts ago, I decided to add a more precise benchmarking category - a sorting algorithm.

Check the live project .

You can check the old post here . The post describes my "benchmarking" afternoon project. The project gave me an idea of some bottlenecks when developing locally, but not much beyond that. It didn't actually compare performance of Go against performance of PHP. Which is why I added the 4th category for benchmarking - a sorting algorithm. And actually measured the backend performance this time.


On both PHP and Go projects I have a file with 5000 unsorted numbers (same order). When clicking on "Fetch qsort (5000)" the both backends 1) fetch the numbers from file into an array 2) sort them and time the sorting operation 3) return the time it took to sort the given numbers.

It's important to note that the performance measuring still isn't the most precise benchmarking you'll ever come across. And are 3 reasons for it .

1.) The algorithms aren't identical.

The major reason for this is because I copy pasted algorithms from Stackoverflow (as credited in code comments). The minor reason for this is that writing "same" qsort algorithm would be writing smelly code in one of the languages. Either you copy the underlying slice you're trying to sort in Go, or you make a ton of temporary variables in PHP. Of course I could just choose an algorithm that's easy to apply in the same matter in both languages. Maybe next time.

2.) The algorithms run at same-ish time

The requests from "Fetch qsort (5000)" button send requests to Go and PHP at the same time. And since they're on the same physical server, they have to share resources. And chances are that go is already done with the whole thing, when PHP just fetches the numbers from the file. So even when sharing the server's resources, they don't necessarily use them for the same computations.

3.) Go's pivot is random (sept 2017), PHP's isn't

Yeah I really should change the pivot's at least. So they both take same (first?) number as pivot. Soon!

Sources: go | PHP


The results are somewhat inconsistent. There's probably various reasons for this, but the point 2.) and 3.) from last section can probably be attributed for the majority of the inconsistency.

Results for PHP sorting range between 20-50ms (and above if you screw around). While go's sorts same array in roughly 0.8-6ms.

Unlike results from previous post, now it's actually possible to get a rough idea on the performance difference between the languages.

Try it out!

This website uses  Google Analytics  cookies. Beware.