PHP list+explode VS substr+strpos - what is more efficient?

后端 未结 6 1720
迷失自我
迷失自我 2021-01-04 22:24

Sample text:

$text = \'Administration\\Controller\\UserController::Save\';

Task - extract everything before ::

Option 1:

         


        
6条回答
  •  猫巷女王i
    2021-01-04 22:33

    On my system:

    ~/pb$ uname -a && php -v
    Linux hostname 3.2.0-4-amd64 #1 SMP Debian 3.2.46-1+deb7u1 x86_64 GNU/Linux
    PHP 5.4.19-1~dotdeb.1 (cli) (built: Aug 27 2013 00:42:43) 
    Copyright (c) 1997-2013 The PHP Group
    Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies
        with XCache v3.0.3, Copyright (c) 2005-2013, by mOo
        with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
        with XCache Cacher v3.0.3, Copyright (c) 2005-2013, by mOo
    

    I have results:

    ~/pb$ ./test ListVsSubstr
    [============================================================>] 1000 u | 8134 u/s | Est: 0.0 s | Mem: 335.74 KB | Max: 357.96 KB
    [============================================================>] 1000 u | 7808 u/s | Est: 0.0 s | Mem: 336.14 KB | Max: 357.96 KB
    Test name       Repeats         Result          Performance 
    list+explode    1000            0.044890 sec    +0.00%
    substr+strpos   1000            0.052825 sec    -17.68%
    

    Test code here: link. From time to time results slightly different, but list+explode is always faster more than 15%.

    Different systems and PHP versions may have different results. You must check it by yourself and for sure in environment configuration identical to your production.

提交回复
热议问题