1

I need a multi-dimensional pre-filled with a numeric value, as so;

for($m = 0; $m <= 149; $m++){
    for($n = 0; $n <= 99; $n++){
            $pitchDef[$m][$n] = 999;
    }
 }

But would it be faster for PHP to simply read the arrays pre-populated?

In other words is it faster for php to read or create an array?

6
  • 6
    Why not benchmark it? Commented Mar 8, 2013 at 8:16
  • By create do you mean to allocate space for, and populate all dimensions of, the array? Or just the actual allocation? Obviously if you need to create and set a large range of values in an array, it'll be slower than a simple read operation. Commented Mar 8, 2013 at 8:20
  • What kind of performance are you looking for? an array of 149*99 filled with integers is probably not going be creating noticeable performance hits. Do whatever seems more clear and most easy to understand. Commented Mar 8, 2013 at 8:20
  • @deceze That's a fair enough comment but also SE is also about creating a repository of info, if I benchmark this and then need it much larger shall I keep on benchmarking? I can also try and learn why, no? Commented Mar 8, 2013 at 8:22
  • @jasonlarke, if the array was allocated for already what would be the theoretical differences in performance? Commented Mar 8, 2013 at 8:38

3 Answers 3

1

Every assigning a new variable to memory requires first to find out how memory is needed, then allocate it from the system and then write. In these three steps the writing itself (the last operation) is the same (I suppose) time as reading, however the whole assignment is longer, because it has two more steps.

If you have your array, in this example, you don't even say how large the array is, so every single writing PHP requires to change structure of the whole array, then allocate memory for an element and then write it.

At least you could at the very beginning allocate memory for the array like ExplesionPills wrote, then access the array.

The best idea is to compare ("benchmark") two methods by making them run many times ("many" is for example 1 million) and see execution times.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks, +1 for the correct considerations (benchmarking, memory allocation) but still not sure if read v write has time difference
It has, because each writing has more steps to proceed than reading.
If you are sure then state that in the answer and I'll accept it.
I won't, it's clear enough. I don't care if you accept it or not, I just tried to explain and help you. My opinions aren't less or more true if written here or there.
"The writing itself is the same (I suppose) time as reading." != "It has(time difference), because each writing has more steps to proceed than reading." ha ha - your answer has an opinion but your comment asserts a fact, I can't accept an opinion anyway if that's what your answer is. And I appreciate the help! Thanks again.
|
1
array_fill(0, 150, array_fill(0, 100, 999));

I'm pretty sure that's as fast as you can get .. it's much faster than two for loops, but both are still trivially fast when run individually.

1 Comment

+1 for the tip - but I didn't 'learn' the answer, thanks though, BIG time saving with array_fill!!!
1

This might help you array_fill()

1 Comment

+1 for the tip - but I didn't 'learn' the answer, thanks though, BIG time saving with array_fill!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.