Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing array better via numeric or associative key?

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

like image 921
ab_dev86 Avatar asked Dec 22 '22 00:12

ab_dev86


1 Answers

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.

like image 161
Jon Avatar answered Dec 24 '22 03:12

Jon