Is array access better with a numeric or associative key?

I iterate over the array of arrays and access the value of the array through associative keys, this is a piece of code. Note: I never iterate over a full array, but only with window 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 kind of page for the web interface. I get windows_start value and display hte next 10 values.

Conceptual implementation:

  • Get windows_start number
  • Start the loop by entering the window_start-TH array of the external array
  • Display the value of an internal array field using an associative index
  • Move to window_start + 1

Internal arrays have about 40 fields. An external array can grow dramatically because it contains a database table. Now I see that as the external array grows, executing on windows 10 takes more and more time.

I need a “performance code” for my code:

If I enter the values ​​of internal arrays using a numeric key, can I have better performance? In general, faster access to the values ​​of an array with a numerical index than handling an associative index (string)?

How to enter a random entry ($ array [random_num]) of an array of length N? O (N), O (N / 2), e.g.

Finally, does the iteration speed over the array depend on the length of the array? I mean, I always repeat 10 elements of the array, but how does the length of the array affect my iteration with a fixed length?

Thanks Alberto

+4
source share
2 answers

If I enter the values ​​of internal arrays using a numeric key, can I have better performance? In general, quick access to values ​​of an array with a numerical index than access with an associative index (string)?

There may be a theoretical speed difference for integer vs string-based access (it depends on the fact that the hash function for integer values ​​matters for the value for strings, I did not read the PHP source to get a specific answer), but it will certainly be insignificant.

How to enter a random entry ($ array [random_num]) an array of length N? O (N), O (N / 2), e.g.

Arrays in PHP are implemented through hash tables, which means that the insert is depreciated by O (1) - almost all inserts are O (1), but some can be O (n). By the way, O (n) and O (n / 2) are one and the same; you may want to revise the text for algorithmic complexity.

Finally, the speed of iteration over the array depends on the array length? I mean, I always repeat 10 elements of the array, but how does the length of the array affect my iteration with a fixed length?

No, array length is not a factor.

Performance drops not because of how you access your array, but because you seem to load all the records from your database in order to process 10 of them.

You must transfer the swap logic to the database itself by including offset and restriction in your SQL query.

+8
source

Premature optimization is the root of all evil. Additional numerical and associative arrays have very different semantic meanings and therefore are usually not interchangeable. And last but not least: None. Arrays in PHP are implemented as Hashmaps and access to them using the key is always O(1)

In your case (paginated), it’s much more useful to just display the elements you want to display, instead of extracting them and slicing them later. SQL has a LIMIT 10 OFFSET 20 -syntax for this.

+3
source

Source: https://habr.com/ru/post/1402894/


All Articles