An alternative way to handle large volumn of integer or short string as array in memory efficient way.
An alternative way to handle large volumn of integer or short string as array in memory efficient way., (*1)
Wait, What???, (*2)
Let's say you need to have a large chunk of array containing millions of elements.
First thing anyone would try is to do it with array()
, (*3)
Whats wrong with array()
?, (*4)
There's nothing wrong with array()
, rather it's an awesome tool in php to get things done.
But in reality, It's little bit more memory hungry. To have milions on element in class array
, you would need hundreds of MB memory.
Even though PHP7 restructured array internals, still they eat lots of memory., (*5)
How will Binarray will help me?, (*6)
Binarray is not actually array at all, It will store your values in a single variable and retrieve it when you want.
It does not provide any of other features that array()
provides.
As it stores all data to a single variable, it tends to use less memory., (*7)
But will it be as fast as array()
?, (*8)
There is no solution, only trade-offs, (*9)
Binarray won't be as fast as array at all. As it will eat low memory, in return it will work much slower and eat up cpu usage a little bit more.
But there will be some case where you don't care about slowing down few seconds, Binarray will be handy in that case., (*10)
I still can't find any reason to use Binarray instead of array, (*11)
Yeah, you can't use Binarray instead of array.
Binarray has just set/get functionality, It can't do anything other than that., (*12)
You can use Binarray when -, (*13)
You can't use Binarray when -, (*14)
composer require ataur/binarray
<?php use Ataur\Binarray\Binarray; use Ataur\Binarray\Drivers\IntDriver; use Ataur\Binarray\Drivers\StrDriver; /** * For storing only integers */ $binarray = new Binarray(new IntDriver()); $binarray[] = 1; $binarray[] = 2; $binarray[] = 3; $binarray[] = 4; $binarray[] = 5; foreach($binarray as $key => $value){ echo $value; } /** * For storing only strings within 10 characters */ $binarray = new Binarray(new StrDriver(10)); $binarray[] = 'John'; $binarray[] = 'Doe'; $binarray[] = 'Lizzy'; $binarray[] = 'Trump'; $binarray[] = 'Helen'; foreach($binarray as $key => $value){ echo $value; } /** * To retrieve a value */ $value = $binarray[0]; /** * To remove a value */ unset($binarray[0]); /** * To count total elements */ $total = count($binarray);
Binarray is open-sourced software licensed under the MIT license., (*15)