PHP Classes

Performances

Recommend this page to a friend!

      PHP Classes blog  >  What Are PHP Classes ...  >  All threads  >  Performances  >  (Un) Subscribe thread alerts  
Subject:Performances
Summary:Degradation of performances
Messages:10
Author:Marjan mickoski
Date:2012-03-21 19:28:47
Update:2012-06-25 20:42:49
 

  1. Performances   Reply   Report abuse  
Picture of Marjan mickoski Marjan mickoski - 2012-03-21 20:34:52
It is proven that procedural coding have greatest performances not only in php, but in every programming language.
I agree that OOP is well formed, and easy to understand third party code, but that cant be good reason only.
In your previous responses for the performances, you always reffer that mysql is the slowest part, so we dont need to carry about other performances. But is that true? Of course not, cause mostly the developers create site that are hosted on shared hostings, with limited CPU and memory allocation.
So i will give you example. If you have this:
function addProduct($info);
and you need to create and add few products like this:
$productsList=array();
foreach ($infoArr as $key=>$product){
$productsList[$key]=new Product($product);
$productsList[$key]->addProduct();
}
Regardless the mysql queries, you know how much resources will be spend for this code.Especialy memory consumption if you have for example 20 predefined variables in the class, but you only need 3 in some situations for example.

For end i would say: OOP is not always good. It's good for singular things like mailing class, upload class, facebook class, etc etc, but for real fast and efficient execution in general and complex applications and algorithms,core of the systems, only the procedural coding and nothing else.

  2. Re: Performances   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-03-21 23:03:43 - In reply to message 1 from Marjan mickoski
No, I do not agree. There is no proof that global code will make PHP run faster, especially when you need to access databases in the way.

Database queries are I/O operations. PHP needs to wait hundreds of milliseconds or even seconds for queries to run and return the results. If there is any noticeable difference between using classes and global code, that difference does not exceed microseconds.

The same goes for memory usage. MySQL buffers query results in memory. This means that it waits for all result set rows to arrive and allocates memory to store all rows before it allows PHP scripts to access those rows.

This means that if your retrieve a results set with thousands of rows, all those rows of result data will be using a lot of RAM.

  3. Re: Performances   Reply   Report abuse  
Picture of Marjan mickoski Marjan mickoski - 2012-03-22 00:00:52 - In reply to message 2 from Manuel Lemos
That's why i said regardless the database queries.Leave them aside. And you keep talking about IO operations. Try to work all the time with object and arrays for examples, and you will see the performance differences.
You said there is no proof. If you need proof, just try it yourself.
I know exactly how database is functioning.I agree with you about that.
What i dont agree with you is "noticable differences". There wont be noticable differences in one single request, but run several hundred/thousand concurent requests, and you will see the difference. Sometimes one "if" can save your server. I think you are aware how things works on limited servers with a lot of requests. And if you dont care about resorces, you know what is happening :)

  4. Re: Performances   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-03-22 00:18:40 - In reply to message 3 from Marjan mickoski
Well, you said it is proven but you did not show the proof. It seems contradictory.

I am well aware that database accesses, which are the I/O operations I am talking about are typically the most expensive. That is why it is recommended to cache query results to avoid the overhead of repeated queries.

When you use a caching system, it does really not matter if you use OOP or not to access the database, because query results are cached on the first access, so the database access code is not run again. That is the efficient way to handle with the load caused by many simultaneous requests.

That was already covered in this blog before:

phpclasses.org/blog/post/66-More-de ...

  5. Re: Performances   Reply   Report abuse  
Picture of Mihajlo Siljanoski Mihajlo Siljanoski - 2012-03-22 12:37:37 - In reply to message 4 from Manuel Lemos
Hi Manuel,
I think you don't understand comments of Marjan. This is very clear..
Caching can be implemented in OOP and in procedural too.. You talking about solution how to resolve performance problems.

If you write the same code in OOP way and in procedural too, you can see the difference. The procedural programming in large code and complex business logic is very hard to understand but is faster than OOP.

  6. Re: Performances   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-03-22 23:37:27 - In reply to message 5 from Mihajlo Siljanoski
Sorry that was not my point.

What I am trying to say is 2 things.

One is that differences of performance may not be of OOP versus global code, but rather the way some people write OOP code.

And the second thing is that database accesses can be optimized with caches in such way that, any differences between OOP and global code approaches will be made irrelevant.

  7. Re: Performances   Reply   Report abuse  
Picture of Michael Michael - 2012-03-23 15:14:59 - In reply to message 1 from Marjan mickoski
I personally prefer procedural coding. I notice people in this forum talking about I/O. Well if you use classes you need to do a lot more read/writes than if you use procedural code. I load 5 files for each output page, an index file that works as an entry point and config (bootstrap), a core file that loads frequently used functions, a model file and two view files (template and content). It's my own MVC framework (Model View Core). If I need to access a database i also load a db wrapper OOP class file. If I need to do mailouts I use swift mailer OOP class file. I use these two class files for re-usability, but my Core file is not a class file because it contains functions for many different purposes. This is an interesting article:
toys.lerdorf.com/archives/38-The-no ...
and I think he is correct in what he says.

  8. Re: Performances   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2012-03-23 19:45:21 - In reply to message 7 from Michael
I think you got a wrong idea about what are I/O operations.

Sure, if you load classes from separate files that is one I/O operation, but when you are concerned about performance, you use a cache engine and the class file loads only once, not repeatedly on every request.

When we talk about load I/O operations we are talking about slow ones, like accessing database or remote network servers.

Anyway, I don't know how you are creating your classes that you necessary need a lot more PHP source files than if you use global code.

At least for me, if I have one file with global code, after converting to classes, it results in one file with a class.

Anyway, as I mentioned in the article, if you have global code that is never reused in other places, it is not worth the effort of being converted to a class. But you never know about the future when you may realized that the code could be reusable in a situation that you did not anticipate.

  9. Re: Performances   Reply   Report abuse  
Picture of Paul Paul - 2012-03-27 00:40:29 - In reply to message 8 from Manuel Lemos
"At least for me, if I have one file with global code, after converting to classes, it results in one file with a class."

You have absolutely no idea how to use objects.

Objects are NOT simply wrappers for procedural functions.

You don't know how to write object oriented code, and your ignorance is plain to see.

Don't write articles and pretend to be an expert when you lack a basic understanding of the subject you're talking about. You don't know what you're doing.

  10. Re: Performances   Reply   Report abuse  
Picture of Jose Moreno Jose Moreno - 2012-06-25 20:42:49 - In reply to message 9 from Paul
Paul, saying that about Manuel can only mean that you did not understand the article and its target audience and also shows that you do not check the classes on this site very often and the work Manuel has been doing.
He perfectly knows what he is talking about.

About the "performance":

When you want performance you do what is needed and that can be build using OOP, procedural or by any other means.. the limiting factor is your brain.
And saying that the code to run something will always be faster if it is built procedural against OOP is like saying that we should not buid functions (procedures) just insert inline code everytime is needed does not matter if duplicated, unroll your loops!! ... you will not have to call functions!! or even better buid the program in assembly, pack it in C and make a php extension.

OOP is about usability, structure, flexibility... and more, can you do that using just procedural code? Great for you! This is not a race and you will not win anything but thanks for not trolling around.