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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: narasius at gmail dot com
New email:
PHP Version: OS:

 

 [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-2021 The PHP Group
All rights reserved.
Last updated: Wed Apr 14 09:01:24 2021 UTC