Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,441
    Thanks
    11
    Thanked 305 Times in 304 Posts

    Array memory allocation

    I've got a situation where I need to assign values into an array, potentially several times during code execution. I'm wondering whether more memory is allocated via using array() multiple times, versus assigning into array elements. For example:

    PHP Code:
    $a = array (1,2,3);
    ...
    $a = array (4,5,6);
    ...
    $a = array (7,8,9); 
    as opposed to:

    PHP Code:
    $a = array (1,2,3);
    ...
    $a[0] = 4;
    $a[1] = 5;
    $a[2] = 6;
    ...
    $a[0] = 7;
    $a[1] = 8;
    $a[2] = 9
    Thanks,
    Dave

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    For an overwrite, the latter would be logically more efficient if these were real arrays. The former would require a malloc for each int*, while the latter is merely overwriting that of a[x].

    In PHP though, there are no arrays, these are all hashtables, and offhand I don't know what type of storage is actually in use. PHP is a surprisingly efficient language in how it deals with things, so I would not be surprised if under the hood both result in the same effect. The only thing that would change that would be if $a is assigned without a reference to another variable, in which case you are guaranteed to malloc more data (since PHP works in copy on write context).

  • Users who have thanked Fou-Lu for this post:

    tracknut (02-23-2012)

  • #3
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,441
    Thanks
    11
    Thanked 305 Times in 304 Posts
    I certainly had no idea that a simple linear array like that was treated as a hash table in PHP! Thanks for the input Fou-Lu, I think I can go with "Plan 1" given what you're saying.

    Dave

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,473
    Thanks
    13
    Thanked 361 Times in 357 Posts
    Quote Originally Posted by tracknut View Post
    I certainly had no idea that a simple linear array like that was treated as a hash table in PHP!
    I'd rather say that PHP treats associative and linear arrays the same as it uses the same construct (array()) to define them.

    maybe the closest to a standard array (at least from behaviour) is SplFixedArray.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, neither associative or linear are arrays in PHP. Both are hashtables. splFixedArray will be the closest, but still not an array since its an object:
    Code:
    typedef union _zvalue_value {
            long lval; 
            double dval;
            struct {
                    char *val;
                    int len;
            } str;
            HashTable *ht;
            zend_object obj;
    } zvalue_value;
    A PHP variable has to be one of those unioned values.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •