require和require_once都是PHP函数,开发人员可以使用它们在某个特定的脚本中导入外部PHP文件。你可以根据应用程序的复杂度调用一次或若干次require_once/require。使用require(而不是require_once)可以提高应用程序的性能

测试前准备:

创建4个空的类来模拟主脚本要使用的外部PHP文件脚本。

ClassA.php:

<?php

class ClassA
{ }

ClassB.php:

<?php

class ClassB
{ }

ClassC.php:

<?php

class ClassC
{ }

ClassD.php:

<?php

class ClassD
{ }

使用require_once导入外部文件:

index.php:

<?php

require_once './ClassA.php';
require_once './ClassB.php';
require_once './ClassC.php';
require_once './ClassD.php'; echo "Only testing require_once";

重启服务器,使用ab压力测试工具模拟10000个请求,同一时间有5个并发请求。

ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index.php

测试结果:

D:\phpStudy\Apache\bin>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index.php
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.demo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests Server Software: Apache/2.4.23
Server Hostname: www.demo.com
Server Port: 80 Document Path: /optimization/index.php
Document Length: 25 bytes Concurrency Level: 5
Time taken for tests: 17.446 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2280000 bytes
HTML transferred: 250000 bytes
Requests per second: 573.20 [#/sec] (mean)
Time per request: 8.723 [ms] (mean)
Time per request: 1.745 [ms] (mean, across all concurrent requests)
Transfer rate: 127.63 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 2
Processing: 2 8 90.4 4 2503
Waiting: 1 7 90.5 3 2503
Total: 2 8 90.4 4 2504 Percentage of the requests served within a certain time (ms)
50% 4
66% 5
75% 5
80% 5
90% 5
95% 5
98% 6
99% 6
100% 2504 (longest request)

使用ab工具测试require_once,可以看到相应时间是8.723ms,该脚本每秒支持573.20个请求。

使用require导入外部文件:

index_1.php:

<?php

require './ClassA.php';
require './ClassB.php';
require './ClassC.php';
require './ClassD.php'; echo "Only testing require";

重启服务器,使用ab压力测试工具模拟10000个请求,同一时间有5个并发请求。

ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php

测试结果:

D:\phpStudy\Apache\bin>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.demo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests Server Software: Apache/2.4.23
Server Hostname: www.demo.com
Server Port: 80 Document Path: /optimization/index_1.php
Document Length: 20 bytes Concurrency Level: 5
Time taken for tests: 17.207 seconds
Complete requests: 10000
Failed requests: 0
Total transferred: 2230000 bytes
HTML transferred: 200000 bytes
Requests per second: 581.16 [#/sec] (mean)
Time per request: 8.604 [ms] (mean)
Time per request: 1.721 [ms] (mean, across all concurrent requests)
Transfer rate: 126.56 [Kbytes/sec] received Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 21
Processing: 1 8 86.5 4 2320
Waiting: 1 7 86.5 3 2320
Total: 1 8 86.5 4 2320 Percentage of the requests served within a certain time (ms)
50% 4
66% 5
75% 5
80% 5
90% 5
95% 6
98% 7
99% 7
100% 2320 (longest request) D:\phpStudy\Apache\bin>ab.exe -c 5 -n 10000 http://www.demo.com/optimization/index_1.php
This is ApacheBench, Version 2.3 <$Revision: 1748469 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/ Benchmarking www.demo.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests

使用ab工具测试require,可以看到相应时间是8.604ms,该脚本每秒支持581.16个请求。

require和require_once经济性能对比的更多相关文章

  1. 记录:sea.js和require.js配置 与 性能对比

    最近有点忙,很久无写博客,记录一下之前的配置require.js和sea.js的配置.(有误有望提出 require.js 文件目录 /app(项目使用js) /lib(require.js jq存放 ...

  2. PHP函数include include_once require和require_once的区别

    了解下include.include_once.require和require_once这4个函数: include函数:会将指定的文件读入并且执行里面的程序: require函数:会将目标文件的内容 ...

  3. [原] KVM 环境下MySQL性能对比

    KVM 环境下MySQL性能对比 标签(空格分隔): Cloud2.0 [TOC] 测试目的 对比MySQL在物理机和KVM环境下性能情况 压测标准 压测遵循单一变量原则,所有的对比都是只改变一个变量 ...

  4. 浅谈C++之冒泡排序、希尔排序、快速排序、插入排序、堆排序、基数排序性能对比分析之后续补充说明(有图有真相)

    如果你觉得我的有些话有点唐突,你不理解可以想看看前一篇<C++之冒泡排序.希尔排序.快速排序.插入排序.堆排序.基数排序性能对比分析>. 这几天闲着没事就写了一篇<C++之冒泡排序. ...

  5. Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比

    目前Java中最IO有多种文件读取的方法,本文章对比Stream,NIO ByteBuffer,NIO MappedByteBuffer的性能,让我们知道到底怎么能写出性能高的文件读取代码. pack ...

  6. C正则库做DNS域名验证时的性能对比

    C正则库做DNS域名验证时的性能对比   本文对C的正则库regex和pcre在做域名验证的场景下做评测. 验证DNS域名的正则表达式为: "^[0-9a-zA-Z_-]+(\\.[0-9a ...

  7. [转]require(),include(),require_once()和include_once()区别

    require(),include(),require_once()和include_once()区别 面试中最容易提到的一个PHP的问题,我想和大家共勉一下: require()和include() ...

  8. 开发语言性能对比,C++、Java、Python、LUA、TCC

    一直想做开发语言性能对比,刚好有时间都做了给大家参考一下, 编译类:C++和Java表现还不错 脚本类:TCC脚本动态运行C语言,性能比其他脚本快好多... 想玩TCC的同学下载测试包,TCC目录下修 ...

  9. php+mysql预查询prepare 与普通查询的性能对比

    prepare可以解决大访问量的网站给数据库服务器所带来的负载和开销,本文章通过实例向大家介绍预查询prepare与普通查询的性能对比,需要的朋友可以参考一下. 实例代码如下: <?php cl ...

随机推荐

  1. memset陷阱

    最近在实现差分进化算法的时候,发现数据异常,查了好久,才知道是memset惹的祸! #include <iostream> #include <cstring> using n ...

  2. AOSP5.0换8G eMMC不能开机问题

    AOSP5.0 MT6572平台.用H9TP32A4GDBCPR_KGM这颗4G的eMMC就能够.可是用H9TP65A8JDACPR_KGM这个8G的就开不了机,一直是重新启动.用串口抓LOG发现以下 ...

  3. Ubuntu安装qBittorrent

    qBitTorrent是Ubuntu Linux中最受欢迎的P2P软件之中的一个. 出自一名法国大学生之手的qBitTorrent功能强大.界面精美.操作直观. qBitTorrent是Linux中最 ...

  4. Webservice工作原理及实例

    Web Service工作原理及实例   一.Web Service基本概念   Web Service也叫XML Web Service WebService是一种可以接收从Internet或者In ...

  5. vue+node+mongoDB 火车票H5(四)---完成静态页面

    各项配置都好了,就可以开始写静态页面了,先别急着写,看一下页面又哪些公用的部分可以提取出来的,统一放到components组件文件夹中 header头部文件夹放一些头部常用组件,如首页的banner切 ...

  6. 170122、Netty 长连接服务

    推送服务 还记得一年半前,做的一个项目需要用到 Android 推送服务.和 iOS 不同,Android 生态中没有统一的推送服务.Google 虽然有 Google Cloud Messaging ...

  7. 手动爬虫之糗事百科(ptyhon3)

    一.调用封装的Url_ProxyHelper类,源码如下 import urllib.request as ur class Url_ProxyHelper: def __init__(self, u ...

  8. python项目环境的导出、导入

    导出开发环境 pip freeze > requirements.txt # 文件导出路径 导入环境 pip install -r requirements.txt # pip 则会自动下载安装 ...

  9. 目标检测之R-FCN

    R-FCN:Object Detection via Region-based Fully Convolutional Networks R-FCN的网络结构 一个Base的convolutional ...

  10. 在MySQL数据库的表中可以给某个整数类型的字段赋字符串类型的值