V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
chinachihan
V2EX  ›  PHP

测试 PHP 代码性能

  •  
  •   chinachihan · 2018-03-14 14:51:21 +08:00 · 1892 次点击
    这是一个创建于 2225 天前的主题,其中的信息可能已经有所发展或是发生改变。

    rt: 比如

    foreach($array as $k => $v) {#code}

    ----------------强大分割线----------------------------

    array_walk($array,function(){#code});

    想测试两种遍历集合的速度,一般哪里着手?

    947211232
        1
    947211232  
       2018-03-14 17:09:14 +08:00
    消灭零回复,可以使用 microtime 对比,结束时间-开始时间=执行时间差
    947211232
        2
    947211232  
       2018-03-14 17:09:48 +08:00
    此类问题往后请 google
    Aluhao
        3
    Aluhao  
       2018-03-14 18:09:45 +08:00
    用命令行测试如:

    <?php

    // time php tests.php

    $arr = array();
    for ($i = 0; $i < 500000; $i++) {
    $arr[$i] = $i;
    }

    $tmp = array();
    foreach ($arr as $i) {
    if ($i % 2 == 0) {
    $is_exists = array_key_exists($i, $arr);
    if ($is_exists) {
    array_push($tmp, $i);
    }
    }
    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5375 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 09:31 · PVG 17:31 · LAX 02:31 · JFK 05:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.