Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    is sprintf or implode faster?

    Code:
    	function prepare() {
    		return implode('', func_get_args());
    	}
    So, if I use this instead of sprintf, will it be faster?
    e.g will:
    Code:
    $t = "good";
    $v = "stuff";
    $var = prepare('This text is: ', $t , ' while that text is: ', $v);
    be faster than:

    Code:
    $t = "good";
    $v = "stuff";
    $var = sprintf('This text is: %s while that text is: %s', $t, $v);

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Sprintf is faster by almost 280%. The function call is invalid as well as you can't use func_get_args() in another function call unless the function prototype has been declared as by reference.
    PHP Code:
    function prepare()
    {
        
    $argv func_get_args();
        return 
    implode(''$argv);

    will work however.

    You know if you use microtime you can do these tests yourself to see which is faster >.<
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    Yea... but everytime I do it, I get a number over 1000.

    PHP Code:
    function microtime_start() {
        global 
    $start;
        list(
    $usec$sec) = explode(' 'microtime());
        
    $start = (float)$usec + (float)$sec;
    }

    function 
    microtime_end() {
        global 
    $start;
        list(
    $usec$sec) = explode(' 'microtime());
        
    $end = (float)$usec + (float)$sec;
        
    $end = (float)$end - (float)$start;
        echo 
    $end;

    is what I used.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    microtime has been upgraded nicely, I don't even remember the old school way of doing it. Takes a boolean result to determine if it should return its value as a float:
    PHP Code:
    $start microtime(true);
    sleep(1);
    $end microtime(true);
    printf("Time spent: %f", ($end $start)); 
    Nice and easy
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •