Friday, November 9, 2007

Benchmark of sorting algorithms written in PHP

Here is benchmark of all array sorting algorithms post in the blog. Sorting arrays of 10 000 elements:































Namerandomsortedrsorted
straightInsertion7.481310.009513.68925
binaryInsertion5.464460.0552810.8473
straightSelection21.245221.3422625.2773
bubbleSort22.7624512.4599131.98982
shakerSort18.753520.0039232.69624
heapSort0.118430.125590.11432
quickSortRecursive11.911826.74236.43074
quickSortRecursiveNew7.415347.450587.38413
quickSort0.055550.028450.03096
sort0.008690.004670.005



Different sort algorithms have advantage in certain conditions. So this benchmark can't show the advantage of all of them.

Function quickSortRecursive receives an array as parameter and then creates new static array for the recursion calls. quickSortRecursiveNew uses global array and it sorts random array faster then quickSortRecursive.
The iterative version of quick sort is more then 100 faster then recursive one!!!

The last function is PHP native function for sorting arrays. It so fast because it isn't run through PHP interpreter. Use PHP native functions, they are a lot faster.

The source of benchmark and all of the functions can be found here.

No comments: