I'm writing an application for my research which deals with very big arrays and lots of iterations on their values. For reducing the calculation time, I'm implementing several improvements in the codes. Right now, after several improvements, the calculation time is still a bit high. The array_key()
function is the one that consumes almost half of the calculation time. Is there any better substitute for that?
I'm creating an example which is a simple version (without iteration loops) of what I need to improve:
$bigArray=array_fill(0,1000000,0);
for($i=0;$i<10;$i++){
$rnd=mt_rand(0,1000000);
$bigArray[$rnd]=1;
}
$start=microtime(true);
$list=array_keys($bigArray,1);
$end=microtime(true);
echo $end-$start;
The outcome is something like 0.021490097045898
seconds. Does anybody know a faster way to do this? Even very small improvement will be helpful since this kind of calculation would go for hundreds of thousands of rounds and sometimes the time reaches up to 30 seconds and half of it is for the array_key()
function as described above.
BTW, I'm running the script on a Dual Core (Intel) E8500 @3.16GHz,3.17GHz with 8Gb RAM, the OS is Windows 7 64-bit (just in case).
Thanks in advance.
Credit goes to @Wikken from the comments:
if it's really just 1's and 0's, array_filter() performs about twice as fast as the array_keys() here.
It is really helpful and performs the job fast. Reduced the time by %20 in my case. Thanks Wikken!