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.

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.

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.

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.

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.