SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)

    PHP object benchmarks vs database query

    Hello there, I was wondering how PHP's object creation/method calls benchmark is compared to database query. I've heard that in most PHP applications the real benchmark is associated with database CRUD operations, but still I wanna know how big this benchmark is compared to creating objects/calling methods. Lets say I have one select query to perform(without join), and a script that creates n objects, how big a number n should be so that the time it takes to execute the query is equivalent to creation of n objects? In a word, how many objects instantiation is equivalent to running a DB select query in terms of time/benchmark? What if the query involves a join statement and more complex where clause? Please lemme know if anyone of you have ever done this comparison, thanks.

  2. #2
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    As a gross overgeneralisation, object creation takes orders of magnitude less time than calling out to query a MySQL database.

    That said, it is entirely possible to make your database operations "faster" (most usually, by not involving the database). It is also entirely possible to make object instantiation, or some other use of an object, really, really, really slow.

    To put things in a hopefully more manageable frame of mind, you're effectively asking to compare mammals (like objects, they share similarities between wildly differing specimens) and classical literature (like databases, they share similarities between wildly differing specimens): it just doesn't make sense to pit one against the other.
    Salathe
    Software Developer and PHP Manual Author.

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    338
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Salathe View Post
    As a gross overgeneralisation, object creation takes orders of magnitude less time than calling out to query a MySQL database.
    Yeah I know object creation is magnitudes faster than database queries, but how big if this magnitude. Lets say my framework uses thousands of objects and 10-20 queries, which one will be faster?

    The fact is that in my application I use a string class that creates immutable string objects to replace PHP built-in string literals(I also use Array classes instead of PHP arrays, but performances are comparable so its not a problem). It's about 2-4 times slower than PHP string literal, but I dont know whether this will be a main bottleneck for my application until I actually compare the time it takes to create thousands of string objects to a few Mysql queries. This is why I asked how many object instantiation takes as much time as running a DB query. If the cost of creating string objects aint comparable to the benchmark of running MySQL queries Id say its not a performance bottleneck, otherwise I drop this object oriented string idea since its too expensive on performance.

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Instead of asking us, why don't you run your own tests and run a profiler over code? That will tell you what is slow and what is not.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    54 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    Yeah I know object creation is magnitudes faster than database queries, but how big if this magnitude. Lets say my framework uses thousands of objects and 10-20 queries, which one will be faster?
    There is no absolute A or B answer to that, that we can provide. Profile your code (there is lots of help around if you're unsure how to do that); profile it as it is (or will be) used in the wild, then you'll get the A or B answer that you seem to be looking for, for your code base.
    Salathe
    Software Developer and PHP Manual Author.


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
  •