Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-23-2012, 08:51 PM   PM User | #1
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
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
tracknut is offline   Reply With Quote
Old 02-23-2012, 08:57 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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).
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
tracknut (02-23-2012)
Old 02-23-2012, 09:02 PM   PM User | #3
tracknut
Regular Coder

 
Join Date: Aug 2006
Posts: 891
Thanks: 4
Thanked 205 Times in 204 Posts
tracknut is an unknown quantity at this point
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
tracknut is offline   Reply With Quote
Old 02-24-2012, 07:12 AM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,855
Thanks: 9
Thanked 288 Times in 284 Posts
Dormilich is on a distinguished road
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.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 02-24-2012, 01:33 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:08 AM.


Advertisement
Log in to turn off these ads.