At first, I'll give a link to code: http://ideone.com/6k8R6
On my Intel Core 2 Duo, PHP 5.4.6 result is:
usort: 7.8763520717621
quicksort: 2.9220938682556
(usort
is slower than quicksort
)
But on Ideone result is:
usort: 0.0313699245453
quicksort: 0.0621209144592
(usort
is faster than quicksort
)
I have also checked code at my friend's computer (Intel Core i5, PHP 5.4.4), and usort
was also faster.
My question is: why sometimes quicksort is faster than usort and sometimes usort is faster?
PHP usort speed
I suspect it's because you're using a really small dataset on ideone and that you have implemented quicksort in pure PHP, while usort is a library function. –
Ekaterina
I believe this is actually based upon the CPU due to Parallelization, I remember reading something about this somewhere....hmmm, I think it was wikipedia actually: en.wikipedia.org/wiki/Quicksort but my head would hurt agian to read that through. –
Leavis
To add to that - your data set is four items. That's not even enough to properly demonstrate quicksort! –
Alkalinity
@duskwuff: my mistake, I have tried also on larger data set, but I pasted wrong link. –
Kiss
Tried it with a 20k dataset, on a first gen i7, PHP5.5.3 and I can not confirm that quicksort would be faster, on the opposite. –
Unjaundiced
Quicksort is considered one of the fastest sort algorithms on unsorted data, and the slowest on already sorted (or nearly sorted) data.
Code was the same on each computer and function
set($uzytkownik);
used to set array to unsorted state before every sorting (I apologize for any grammatical errors, but I'm from Poland and it's quite late for me ;)). –
Kiss © 2022 - 2024 — McMap. All rights reserved.