PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
The following code sorts an array and outputs it as string of 5 chars.
Each array member has between 0 and 4 chars. The more characters a member has the earilier in the output string it should be. But if member 4 and meber 6 have the same string length they should appear in the output in that order. The code below works fine but is too slow.
maybe it's faster if you first sort the array via usort and then iterate the array until you have 5 characters.
usort uses the quicksort algorithm, it's quite fast. see http://en.wikipedia.org/wiki/Quicksort
It all depends on the size of input (the number of items to sort)....
In general i would do it as following:
Build pairs of (string value, string length), thus avoiding the overhead of calling strlen during the sorting process itself.
And then sort the pairs using the length attribute.
And then return the values of the pairs.
timvw wrote:Build pairs of (string value, string length), thus avoiding the overhead of calling strlen during the sorting process itself.
php's strlen() is not as costly as the "old C sz-strings" strlen(). The length of a string is stored, it does not need to be calculated each time strlen() is called.
bokehman wrote:I tried usort and it takes 3x longer than the double loop.
Then we're probably not talking about member 4 and 6 but 40000 and 312000
Maybe it's possible to narrow down the search domain before all elements are in that huge array.
bokehman wrote:I tried usort and it takes 3x longer than the double loop.
Then we're probably not talking about member 4 and 6 but 40000 and 312000
Maybe it's possible to narrow down the search domain before all elements are in that huge array.
It is just the 13 item array posted above and it take 160 microseconds compared to 65 with the double loop. Tim's method is taking 30 but I can't get it to work right.
volka wrote:Ah ok. Milliseconds is a scale unit I don't care about Good luck.
I said microseconds not milliseconds and when you are doing a brute force search saving a few microseconds adds up to a lot of time. I rewrote my original function and got down from 3 milliseconds to 156 microseconds of which this loop is taking 65. That's almost half. if I could get Tim's idea to work I could save another 30.
feyd: That's a huge improvement but there is something wrong with my implementation of array_multisort. Notice in your last two results the "3" is out of turn. Any idea what is going wrong?
Other than the output being six characters, it's technically sorted correctly as the character '3' falls before 'K'. That's simple enough to truncate to five characters, if that's what you're worried about.
Do you want numbers stripped out? It's pretty simple to do on the front end with array_filter() and ctype_alpha(), depending on how you want the results...