SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Performance - Array VS Object

    It seems like:
    PHP Code:
    $array['key'] = 'value' 
    ...executes faster than:
    PHP Code:
    $object->key 'value' 
    I'm wondering why is that and if you have any performance optimization tips for objects.

    Thank!

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An object is a richer data type than an array. Because of that, I would expect more overhead since it provides more features. The difference really should be negligible in your example though. Is it not? If you need the functionality of an object, use it.

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! That makes sense.

    Here's another question. What are the basic advantages of objects in terms of functionality? At what point do you say - this should no longer be an array, it should rather be an object?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you usually don't use objects unless you intend to write some code that uses object oriented principals. I realize I just beat around the bush with that answer, but I don't think I could really get the point across in a summary. I don't feel OOP is something people generally understand well until you put in a decent amount of effort studying.

    If you're using an object because you like the syntax of $foo->key better than $foo['key'], my advice is don't.

  5. #5
    SitePoint Enthusiast
    Join Date
    Feb 2007
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your point, but it brings another question. If arrays are better for storing data, why does the mysql_fetch_object method exist and why would anyone use that method over mysql_fetch_assoc?

  6. #6
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    An object will have a slight overhead compared to an associative array, mostly because you need to also dereference the 'this' pointer upon access.

    As for memory allocation, there shouldn't be much of a difference, although I have heard others claim there was.

    Like it's been said, the performance gain would be negligable and not worth worrying about. Use objects their cleaner
    The only constant in software is change itself

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you use objects rather than array, you can better enforce a structure, and generating docs about it is also easier. I try to use objects in that case, although nothing beats the speed of arrays, so I still use arrays a lot.


Bookmarks

Posting Permissions

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