Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is calling function_exists() faster or slower that apply_filters() ... or is the difference so small that it should not be considered?

I did a bit of testing based on Kaiser's and it showed that function_exists() is ~3 times faster where both the function and filter exist. and ~11 times faster if the filter does not exist. Was not expecting this.

function taco_party() {
    return true;
}

add_filter( 'taco-party', 'taco_party' );

timer_start();
for ( $i = 0; $i < 1000000; $i++ )  {
    $test = apply_filters( 'taco-party', '' );
}
echo( 'Seconds: ' . timer_stop( 0, 10 ) . '<br />' );

timer_start();
for ( $i = 0; $i < 1000000; $i++ )  {
    if ( function_exists( 'taco_party' ) ) {
        $test = taco_party();
    }
}
echo( 'Seconds: ' . timer_stop( 0, 10 ) . '<br />' );

Bear in mind that this is running each method 1,000,000 times which is quite a lot. Each method ran once completes very, very quickly:

Test 1: 0.0000491142
Test 2: 0.0000140667

I would conclude that the difference is not an issue.

share|improve this question
2  
Aren't they two different things? If you want to know if calling a function directly is faster than calling it trough a wrapper function like call_user_func, then obviously yes, but the differences are negligible. –  onetrickpony Jun 24 '11 at 14:57
    
Totally, but in the context of a theme using a plugin's functionality these 2 different methods are used to accomplish the same task. –  mfields Jun 24 '11 at 15:05
    
Now, if you put those numbers in the context of an overall query (much less, a pageload), I'm guessing that the difference (30 micro seconds) is not statistically significant. –  Chip Bennett Jun 24 '11 at 20:56
    
Situations in which you need to make sure a function exists are pretty rare; most of the times you know it exists and just call it –  onetrickpony Jun 24 '11 at 20:56
    
Wait: is this even a real-world test? Shouldn't you compare over-riding a function to applying a filter? –  Chip Bennett Jun 24 '11 at 21:00
show 2 more comments

3 Answers

I don't know if one is faster or slower than the other, but I would suggest that using apply_filters() is the better approach, because it is cleaner, and more intuitive (in the sense of doing things the "WordPress way").

EDIT

If you're doing comparison tests, shouldn't you compare the time required to execute the following:

This:

<?php
if ( ! function_exists( 'taco_party' ) ) {
    function taco_party( $salsa = true ) {
        return $salsa;
    }
}

function taco_party( $salsa = true ) {
    return $salsa;
}
?>

Versus This:

<?php
function taco_party( $salsa = true ) {
    return apply_filters( 'taco-party', $salsa );
}
function hot_salsa() {
    $salsa = true;
    return $salsa;
}
add_filter( 'taco-party', 'hot_salsa' );
?>

It's not just the time required to check for the existence of the function or filter, but rather the time required to do something.

share|improve this answer
1  
Totally agree Chip! Someone asked if one was more resource intensive than the other and I really didn't have an answer. –  mfields Jun 24 '11 at 15:06
1  
We're actually taking about 2 different applications of function_exists(). Your test is good for "maybe defining a function in functions.php if the function has not been defined in a child theme". My test is for assigning a function to a filter in a plugin and using the filter inside the theme to display data. I believe that my test checks what it needs to. But really, we are talking about 2 different things here :) –  mfields Jun 24 '11 at 21:36
    
Okay, I gotcha; you're thinking specifically in the context of a Plugin, that requires something to be added to the template, but can't use a standard/core hook to do so: create a template tag (that gets wrapped in a function_exists() conditional), or define a custom filter. And in my case, I'm thinking in the context of a Theme that is designed to be over-ridden. –  Chip Bennett Jun 24 '11 at 22:43
add comment

Disclaimer: The numbers are out of a non-vanilla install, so it could be less on both sides

WordPress has ~ 2.700 functions & ~ 250 attached actions in global $wp_filter;

So the Q could be:

Is iterating through the (at this point) existing functions faster than iterating through the array of hooked filters?

Imo filters/hooks are the faster way, more safe and easier to backtrace. My choice would be filters.

share|improve this answer
1  
filters/hooks are the faster way => and this statement is based on... ? I highly doubt it they are faster, but as I said the difference in speed insignificant –  onetrickpony Jun 24 '11 at 17:29
    
"Imo" means: "In my opinion", which is a guess based on the amount. –  kaiser Jun 24 '11 at 18:07
add comment

Ok, after running several tests1) I can see that has_filter takes ~ 20% more time to finish (on a not vanilla install) than function_exists:

// You have to uncomment one or the other to run the test
// don't run timer_xy() functions behind each other - the result is then wrong
timer_start();
$output = ' false ';
for ( $i = 0; $i < 1000000; $i++ ) 
{
    // if ( function_exists('wp_login') ) $output = ' true ';
    if ( has_filter( 'shutdown' ) ) $output = ' true ';
}
echo $output;
echo('Seconds: ' . timer_stop(0,10) . '<br />');

1) This is the code I used to test. It was set on the top of the functions.php file

share|improve this answer
    
Thanks for running the test! The question was comparing apply_filters() to function_exists() not has_filter(). –  mfields Jun 24 '11 at 19:58
    
N/P. Could you please update your Q with the actual code you're using? Hard to imagine your use case at the moment. –  kaiser Jun 24 '11 at 20:03
    
The use case, as far as I understand it, is comparing the use of apply_filters() vs. a pluggable function, for Themes designed to allow themselves to be overridden by Child Themes. So, if a Theme adds some content via function, it could wrap that function in a function_exists() call, or it could wrap the content inside the function in an apply_filters() call. –  Chip Bennett Jun 24 '11 at 20:10
    
Chip, That's one use case for sure. Another is a plugin providing filters as an API to access data/functionality. –  mfields Jun 24 '11 at 20:23
    
Ok. But that's hardly comparable. I took the nearly latest loaded function and the latest hook to compare. How would you setup a test case? –  kaiser Jun 24 '11 at 20:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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