5

I iterate over an array of arrays and access the array's value through associative keys, this is a code snippet. Note: i never iterate over the total array but only with a window of 10.

//extract array from a db table (not real code)
$array = $query->executeAndFetchAssociative;

$window_start = 0;

for($i = $window_start; $i<count($array) && $i<$window_start+10; $i++)
  echo($entry["db_field"]);

This is a sort of paginator for a web interface. I receive the windows_start value and display hte next 10 values.

A conceptual execution:

  1. Receive the windows_start number
  2. Start the cycle entering the window_start-TH array of the outer array
  3. Display the value of a field of the inner array via associative index
  4. Move to window_start+1

The inner arrays have about 40 fields. The outer array can grow a lot as it rapresent a database table. Now i see that as the outer array gets bigger the execution over the windows of 10 takes more and more time.

I need some "performance theory" on my code:

If I enter the values of inner arrays via numeric key can I have better performance? In general is quickier accessing the array values with numeric index than accessing with associative index (a string)?

How does it cost entering a random entry ($array[random_num]) of an array of length N ? O(N), O(N/2) just for example

Finally the speed of iterating over an array depends on the array lenght? I mean i always iterate on 10 elements of the array, but how does the array lenght impact on my fixed length iteration?

Thanks Alberto

1
  • 1
    A general optimization when using loops: move maths stuff (count, $window_start+10) out of the loop; do the calculation just once before the loop and not in every iteration. This should not matter in your case as you would not have any calculations anymore if you follow the answers below. Commented Mar 22, 2012 at 14:01

2 Answers 2

8

If I enter the values of inner arrays via numeric key can I have better performance? In general is quicker accessing the array values with numeric index than accessing with associative index (a string)?

There might be a theoretical speed difference for integer-based vs string-based access (it depends on what the hash function for integer values does vs the one for string values, I have not read the PHP source to get a definite answer), but it's certainly going to be negligible.

How does it cost entering a random entry ($array[random_num]) of an array of length N ? O(N), O(N/2) just for example

Arrays in PHP are implemented through hash tables, which means that insertion is amortized O(1) -- almost all insertions are O(1), but a few may be O(n). By the way, O(n) and O(n/2) are the same thing; you might want to revisit a text on algorithmic complexity.

Finally the speed of iterating over an array depends on the array length? I mean i always iterate on 10 elements of the array, but how does the array length impact on my fixed length iteration?

No, array length is not a factor.

The performance drops not because of how you access your array but because of the fact that you seem to be loading all of the records from your database just to process 10 of them.

You should move the paging logic to the database itself by including an offset and a limit in your SQL query.

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

1 Comment

Thanks for answering that's clear to me: pagination should be moved to database. Apart from pagination task i was seraching for some "php array performance theory"...
3

Premature optimization is the root of all evil. Additional numeric and associative arrays have a very different semantic meaning and are therefore usually not interchangeable. And last but not least: No. Arrays in PHP are implemented as Hashmaps and accessing them by key is always O(1)

In your case (pagination) it's much more usefull to only fetch the items you want to display instead of fetching all and slicing them later. SQL has the LIMIT 10 OFFSET 20-syntax for that.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.