I need to set an array from 1 to 2 million for my current project. The problem is that I get a memory error when I try to execute…
Edit:
This is for a ProjectEuler problem which I have already solved. However, I am refactoring the code to do it much faster (original time was ~90 secs).
I’m using the sieve of eratosthenes to mark the primes from 0 to 2mil. Wikipedia article on this
EndEdit
<?php
range(0,2000000);
?>
obviously if this won’t work, the rest of the code won’t work.
Any ideas?
P.S. I tried ini_set('memory', '30M'); , but that didn’t work either.
Edit
Thanks to all who pointed out that the memory would need to be much larger than I first anticipated. I solved the problem by throwing in ini_set('memory_limit', '4000000000'); //about 3.73 gig
EndFinalEdit
If you can set at arbitrary place and get data from arbitrary index there is no point of sequential array.
If you are solving this euler problem in console (what I usually do), then run the php file with
-d memory_limit=4Gparameter. It’ll run with 4 GB of memory.BTW You can create some magic class which can mimic that you have a big array like that. 🙂