1

If was wondering about PHP performance on Wordpress,

If I had many duplicate add_action.

add_action( 'init', 'func_a' );
add_action( 'init', 'func_b' );
add_action( 'init', 'func_c' );
//... so on

vs

add_action( 'init', 'func_init' );
function func_init() {
   function_a();
   function_b();
   function_c();
   //... so on
}
  • The first one should be a problem with performance if I have many same actions ?

  • Any best way to do (readable with good performance)?

  • When wordpress executes your action it calls call_user_func_array. I think you should check the performance of call_user_func_array vs a regular calling of a function. In my opinion it depends on the logic of your program. – Laxmana Nov 10 '16 at 10:33
2

Well, the short answer is the first item consumes more memory then the second one. One of the reason would be that WordPress hooks uses call_user_func_array() to call al those functions dynamically. And call_user_func_array() actually takes an array as one of its parameter and do a search for a function with this name. So in run time it causes memory and time. You can check the menory and time difference using e.g. memory_get_usage() and timer_stop(). Many good plugins out there to help with that.

But already many other core WordPress functions and other plugins hooking themselves and their functions to that hook. So your three functions will be just three new element in that array and I think it's not gonna be performance issue. And if you think you wanna reduce the consumed time and memory caused by your three function, keep in mind that it is too little to ignore based on code maitainability. The first way of your's is pretty readable as well as maitainable. So I think you can ignore the performance issue unless it really grows into something which should not be ignored (Which is not gonna happen :) ).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.