Data structure is simple logical representation and organization of data inside the main memory. These are used to organize data in a way that the insertion,searching and deletion is performed with minimum complexity at the same time ensuring efficient computing performance.
Array in PHP is one such super awesome data structure that is also hybrid and maintain the behavior of a list and a linked map. Flexibility of Array has limited amount of complexity.
Let explore PHP 7 data structures based on performance.
Data structure Vector represents a series of values in adjacent buffer that grows and constrict on its own.It has extremely low memory usage and get, set, push and pop are O(1). a disadvantage of this structure is it has insert, remove, shift, and unshift as O(n). It is an excellent sequential structure due to a value index being a direct mapping to its index in the buffer
Data structure Stack is based on last in first out aka LIFO concept. It allow access to top values in the structure and further iterates in similar manners. The following figure displays the time taken to do 2ⁿ pop operations
Queue is based on “first in, first out” or “FIFO” concept that allows access only to the value present at the front of the queue and keep iterating in that order. An internal Ds\Queue is used by Ds\Queue.
It is an alternative of spl_object_hash that on behalf of handle defines an object’s hash. It means if two objects considered equal by an implicit definition won’t be treated equal since they don’t belong to same instance. Hashable has two methods: hash and equals. Data structures honouring Hashable interface are Map and Set .
A sequential collection of key-value pairs that is very identical to an array when implemented in similar scenario. Type of key can be any but key value pairs is supposed to be unique. Values will be replaced if added to map using the same key.
-Performance and memory efficiency is as good as in an array.
-In case of size drop is sharp low allocated memory will be allocated automatically
-Keys and values can be of any type even including objects.
-Put, get, remove, and hasKey are all O(1)
-When objects are used as keys Map cannot be converted into an array.
In the below figure an array will hold up allocated memory whereas, Ds\Map will free allocated memory unlike, Ds\Map will free allocated memory when the size drops a certain capacity. Although memory efficiency and performance are quite similar between an array and Ds\Map
Read More […]
About Singsys Pte. Ltd. Singsys is a solution provider that believes user friendly and industry ready solution to engage customers and boost your brand online results from a set of certified developers, designers who are expert in optimized utilization of the available resources to align client’s idea into a mobile application, web application or an E-commerce solution.
You may be interested in following:
1- PHP, PYTHON or RUBY an in depth comparison