0

in many languages like c#, java, etc, we have sorted array! it mean when we add some element to it, it will add new element in correct index base on sorting method ... with this algorithm inserting and element is too fast !

but in php as i know, we can just sort array with ksort,sort or something like that and i did't find something like sorted array in php for better performance in managing one array that we want always be sorted!

in my tough if i figure out how ksort works, i could write sorted array myself but i did't find any solution to add item to array by index ...

does anyone know how to implement sorted array in php ? Or does anyone know how to add element in any index i want?


update 1

i need something like this in php : https://www.tutorialsteacher.com/csharp/csharp-sortedlist

i have huge array (about 2m rows) and i should keep it sorted base on something like price !

for now i sorting it by ksort but i cause me huge impact on performance


Update 2

i want to create trade matching engine for my crypto currency exchange and for now i using redis as my data entry but when i used in php array as my data entry, performance increase about 250x.

(in redis i can add orders about 3.5k/s)

i tested my code and figured out that ksrot take about 83% of my code runtime. now i want to use sorted list as i say for improving my performance. for now i can handle about 930k/s orders(add in array , sort array, iterate top of array) with ksort.

Mahdi Youseftabar
  • 2,273
  • 1
  • 22
  • 29
  • 1
    _how to add element in any index i want?_ ...`$arr[45] = "something";` – ADyson May 02 '21 at 21:22
  • 2
    What about using heaps from the [SPL extension](https://www.php.net/manual/en/spl.datastructures.php)? There's an [example](https://www.php.net/manual/en/class.splheap.php#93930) in the comments – PtrTon May 02 '21 at 21:25
  • https://stackoverflow.com/questions/3797239/insert-new-item-in-array-on-any-position-in-php – Artier May 02 '21 at 22:02
  • @ADyson `$arr[45] = "something"` it will add `something` to key 45 not index of 45 of array ... for example i want add an element to 2 ! it mean when i use foreach on array, i getting that element in second iteration – Mahdi Youseftabar May 05 '21 at 08:24
  • @PtrTon if i use heap for that i just can add element in end of heap ! i want to add my element everywhere i want ! `public SplHeap::insert ( mixed $value ) : void` – Mahdi Youseftabar May 05 '21 at 08:26
  • @Artier It's correct but if i has huge array with high add or remove actions , i should split and merge arrays in every delete and add action , it cause huge cpu impact .... – Mahdi Youseftabar May 05 '21 at 08:27
  • in c# we have sortedList https://www.tutorialsteacher.com/csharp/csharp-sortedlist i just something like this ! – Mahdi Youseftabar May 05 '21 at 08:29
  • @MahdiYouseftabar you would have to implement the sorting yourself, such that when you do as I suggested, you would then sort the array (or at least, sort it before outputting). There's nothing built-in in PHP to do exactly as you wish. Maybe someone already made a library for it though, you would have to [search like this](https://www.google.com/search?q=php+sortedlist). – ADyson May 05 '21 at 08:40
  • @ADyson all of packages using `sort` method ! for example https://github.com/malarzm/collections/blob/master/lib/Malarzm/Collections/SortedList.php i think i should use linked list as data structure and base on that implement this ! – Mahdi Youseftabar May 05 '21 at 09:05
  • Just thoughts: Have you considered using an 'SQLite' database for this structure? If will be fast as it is in memory. As it uses indexes then it is kept sorted. What I am really saying is that PHP arrays may not be the most appropriate tool for what you are trying to do. All PHP installations come with SQLite as standard as far as I know. – Ryan Vincent May 05 '21 at 11:48
  • @RyanVincent dudeeeeeeee !!! using any db (really any you say !) atleast add 250x time to any operation ! i tried redis sorted set but with i got por performance :( – Mahdi Youseftabar May 06 '21 at 23:42
  • Databases are extremely efficient at ordering - unless you don't configure the indexes properly – ADyson May 07 '21 at 08:13
  • Would you mind adding some base sample data and sample insert date to your question and what you want to index it on. Also, what rate of access and inserts per second that you require? I haven't benchmarked this before but I am wondering how fast an 'in-memory' SQLite db with prepared queries will perform. This is an excuse to waste a couple of hours and have fun. :) p.s. Have you had a look at 'SQLite? The communication channel isn't the 'bottleneck' as it isn't a 'server'. – Ryan Vincent May 07 '21 at 16:35
  • @ADyson i add more details ... i worked with sqlite and i know, redis is faster than that .... – Mahdi Youseftabar May 07 '21 at 21:49
  • I was thinking more of a traditional heavyweight RDBMS really, given the volumes of data you're talking about. Sql Server, Oracle, maybe mySQL could do it – ADyson May 07 '21 at 21:52

1 Answers1

1

finally i found the solution and it was: Priority Queue in SPL EXT https://www.php.net/manual/en/class.splpriorityqueue.php

you can pass your compare method to it and it will store it sorted!

i will create some sort of benchmark for compare using usort and priority queue solution for more accurate decision :)

tnx @PtrTron for suggestion this

Mahdi Youseftabar
  • 2,273
  • 1
  • 22
  • 29