php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64084 Using exceptions is much slower than using function-approach
Submitted: 2013-01-27 12:17 UTC Modified: 2013-01-27 14:10 UTC
From: narasius at gmail dot com Assigned:
Status: Wont fix Package: Performance problem
PHP Version: 5.4.11 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
16 + 42 = ?
Subscribe to this entry?

 
 [2013-01-27 12:17 UTC] narasius at gmail dot com
Description:
------------
In speed-sensible application this issue might affect the efficiancy. It appears that using an exception to report some error is at least 10 times slower (with recursion of 3) that using old-fashioned function-approach.

For test-script below timing for using exception is 2.962 seconds, for using function-approach timing is 0.336 seconds.

Test script:
---------------
<?php

    function exceptionf1()
    {
        throw new Exception( 'msg' ) ;
    }
    function exceptionf2()
    {
        exceptionf1() ;
    }
    function exceptionf3()
    {
        exceptionf2() ;
    }


    function returnfunction1( &$error_msg )
    {
        $error_msg = 'msg' ;
        return false ;
    }

    function returnfunction2( &$error_msg )
    {
        return returnfunction1( $error_msg ) ;
    }

    function returnfunction3( &$error_msg )
    {
        return returnfunction2( $error_msg ) ;
    }




    $t1 = microtime( true ) ;
    for ( $i = 0 ; $i < 1000000 ; $i++ ) {
        $success = true ;
        try {
            exceptionf3() ;
        } catch ( Exception $e ) {
            $success = false ;
            $error_msg = $e->getMessage() ;
        }
    }
    $d = microtime( true ) - $t1 ;

    print "d=$d\n" ;





    $t1 = microtime( true ) ;
    for ( $i = 0 ; $i < 1000000 ; $i++ ) {
        $success = returnfunction3( $error_msg ) ;
    }
    $d = microtime( true ) - $t1 ;
    print "d=$d\n" ;



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-01-27 14:10 UTC] rasmus@php.net
Yup, exceptions are not super fast. But assuming you are not using exceptions for 
flow-control and they only happen in really exceptional cases, it shouldn't be an 
issue.
 [2013-01-27 14:10 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Wed Feb 01 06:05:54 2023 UTC