zoukankan      html  css  js  c++  java
  • PHP 关于字符串插值,和单引号字符串连接的性能测试。

    一早上看了篇博客插值,做了下性能测试。发现有悖于常论。

    <?php
    header("Content-type:text/html;charset=utf-8");
    class test{
    	function getMillisecond() { 
    		list($s1, $s2) = explode(' ', microtime()); 
    		return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000); 
    	}
    }
    
    $test = new test();
    $array = null;
    $tempStr = "abc";
    $start = $test->getMillisecond();
    for ($i=0; $i < 10000000; $i++) { 
    	$array[$i] = 'abcd'.$tempStr.'!';
    	unset($array[$i]);
    }
    $end = $test->getMillisecond();
    $res = $end-$start;
    echo '这是字符串拼接的所花时间'.$res;
    
    $array = null;
    $start = $test->getMillisecond();
    for ($i=0; $i < 10000000; $i++) { 
    	$array[$i] = "abcd${tempStr}!";
    	unset($array[$i]);
    }
    $end = $test->getMillisecond();
    $res = $end-$start;
    echo '这是插值所花时间'.$res;
    
    ?>
    

     以上是测试代码。

      测试结果是:

         第一次:  这是字符串拼接的所花时间12203这是插值所花时间11227

         第二次:  这是字符串拼接的所花时间12130这是插值所花时间11249

         第三次:  这是字符串拼接的所花时间12127这是插值所花时间11217

         .....  

       应该不是偶然。代码逻辑有问题么?能说明问题么?能当测试用例么?

    积累知识,分享知识,学习知识。
  • 相关阅读:
    restfulframework详解
    restful规范
    02-模板字符串
    01-学习vue前的准备工作
    vue系列
    crawler_编码转换_unicode(&#24180;)
    002-算法-递归法
    001-算法-递推法
    000-算法-基础概念
    linux_后台启动多个java -jar 程序,及关闭
  • 原文地址:https://www.cnblogs.com/bin-pureLife/p/4015089.html
Copyright © 2011-2022 走看看