To continue on with the previous post, On the speed of functions and namespaces, it was brought to my attention that namespaces should be faster with aliasing. Also how does it compare to static functions? The news was not good for either. Another thing that was brought to my attention that to avoid a lookup when in a namespace functions like filter_var should be absolute referenced, so we/I tried that too.

This version runs the global spaced version, normal namespace call, absolute namespace call, aliased namespace call, aliased subnamespaced call, static method call, and static method on a class in a namespace. The numbers vary depending on your processor strength, but the ratios hold pretty solid regardless of the power.

PHP Code:
<?php

/*

Jaina:Tools bob$ php strtobool4-test.php
strtobool: 0.117sec
zen\util\strtobool: 0.206sec
\zen\util\strtobool: 0.203sec
zurr\util\strtobool: 0.203sec
zurrutrr\strtobool: 0.203sec
zenutil::strtobool: 0.288sec
zen\util\durr::strtobool: 0.411sec

*/

namespace zen\util {
    class 
durr {
        static function 
strtobool() {
            return \
filter_var($input,FILTER_VALIDATE_BOOLEAN);
        }
    }
    
    function 
strtobool($input) {
        return \
filter_var($input,FILTER_VALIDATE_BOOLEAN);
    }
}


namespace {
    use 
zen as zurr;
    use 
zen\util as zurrutrr;

    class 
zenutil {
        static function 
strtobool() {
            return 
filter_var($input,FILTER_VALIDATE_BOOLEAN);
        }
    }
    
    function 
strtobool($input) {
        return 
filter_var($input,FILTER_VALIDATE_BOOLEAN);
    }
    
    
$a 0;
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
strtobool('False');
    }
    
printf("strtobool: %.3fsec\n",(gettimeofday(true)-$start));    
    
    
$start 0;
    
$a 0;    
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
zen\util\strtobool('False');
    }
    
printf("zen\util\strtobool: %.3fsec\n",(gettimeofday(true)-$start));

    
$a 0;    
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        \
zen\util\strtobool('False');
    }
    
printf("\zen\util\strtobool: %.3fsec\n",(gettimeofday(true)-$start));

    
$a 0;
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
zurr\util\strtobool('False');
    }
    
printf("zurr\util\strtobool: %.3fsec\n",(gettimeofday(true)-$start));

    
$a 0;
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
zurrutrr\strtobool('False');
    }
    
printf("zurrutrr\strtobool: %.3fsec\n",(gettimeofday(true)-$start));
    
    
$a 0;
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
zenutil::strtobool('False');
    }
    
printf("zenutil::strtobool: %.3fsec\n",(gettimeofday(true)-$start));    

    
$a 0;
    
$start gettimeofday(true);
    for(
$a 0$a 100000$a++) {
        
zen\util\durr::strtobool('False');
    }
    
printf("zen\util\durr::strtobool: %.3fsec\n",(gettimeofday(true)-$start));
}

?>

tl;dr

  • native functions are fast.
  • global namespace functions are decent.
  • namespaced functions are slow.
  • static methods are slower.
  • namespaced static methods, you might as well give up expecting haste.