zoukankan      html  css  js  c++  java
  • php性能优化学习笔记

      编写代码

    1.尽可能多的使用内置函数
    2.比对内置函数的时间复杂度,选择复杂度低的 比如 循环20万次-测试isset 和 array_key_exists 耗时
    对比isset.php , array_key_exists.php
    3.尽可能少用魔法函数
    对比__get.php no__get.php
    4.最好不用@错误抑制符号
    查看at.php , 利用vlb扩展查看opcode
    1 cli: php -dvld.active=1 -dvld.execute=0 at.php 查看opcode
    2 
    3 //观察下面2行的opcode
    4 file_get_contents('1.txt');
    5 @file_get_contents('1.txt'); //opcode 多了些逻辑 
    5.合理使用内存和正则表达式
    利用unset()及时释放不适用的内存,尽量少用正则表达式
    6.减少计算密集业务
    大数据计算,日志分析等
    7.务必使用带引号字符串做键值
    查看arr.php
     1 <?php
     2 //define('key','imooc');
     3 $arr = [
     4     'key'=>'hello',
     5     'imooc'=>'http://www.imooc.com/'
     6 ];
     7 
     8 echo $arr[key];
     9 /*
    10 key没加引号
    11 key首先会去寻找 常量key,如果有则取常量key的值,然后取回数组对应的值。
    12 如果没找到,则解析成数组内部键
    13 */ 
    8. php周边问题的性能优化
    linx服务器优化,文件存储,内存缓存,网络
    减少网络请求:
    a.可以使用curl_multi_*()
    b.使用swoole扩展 

    9.php做接口, 压缩php接口输出 如gzip
    利:有利于client端更快接收数据;
    弊:额外的cpu开销
    结论:小于几十kb压缩效果不好,大于100k可以尝试

    10. 利用xhprof性能测试
    查看xhprof.php
    参考: http://avnpc.com/pages/profiler-php-performance-online-by-xhprof
     1 <?php
     2 //参照 xhprof项目里的 example/simple.php
     3 
     4 #开启
     5 xhprof_enable();
     6 
     7 #脚本执行完
     8 register_shutdown_function(function(){
     9 
    10     $data = xhprof_disable();
    11 
    12     require 'xhprof_lib/utils/xhprof_lib.php';
    13     require 'xhprof_lib/utils/xhprof_runs.php';
    14 
    15     //生成分析文件
    16     $obj = new XHProfRuns_Default();
    17     $run_id = $obj->save_run($data,'test');
    18     var_dump($run_id);
    19 
    20 });
    11. 开启opcache
    php执行流程
    1 *.php脚本被底层的zend引擎逐行扫描分析 保存成zend自己能识别的语法
    2 zend自己能识别的语法然后再被解析成opcodes (最终将要被执行的代码)
    3 最后执行opcodes 输出
       附上php执行流程图
      

      文中提到文件下载:http://files.cnblogs.com/files/loveyouyou616/yh.zip

     
  • 相关阅读:
    leetcode刷题29
    leetcode刷题28
    leetcode刷题27
    leetcode刷题23
    leetcode刷题22
    leetcode刷题21
    leetcode刷题20
    Unity中通过DoTween实现转盘效果
    U3D工作注意事项,不要再犯!
    Unity中String字符串的优化
  • 原文地址:https://www.cnblogs.com/loveyouyou616/p/5566672.html
Copyright © 2011-2022 走看看