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. uva 10494 - If We Were a Child Again 大数除法和取余

    uva 10494 - If We Were a Child Again If We Were a Child Again Input: standard inputOutput: standard ...

  2. poj 2987(最大权闭合图+割边最少)

    题目链接:http://poj.org/problem?id=2987 思路:标准的最大权闭合图,构图:从源点s向每个正收益点连边,容量为收益:从每个负收益点向汇点t连边,容量为收益的相反数:对于i是 ...

  3. 定制iOS 7中的导航栏和状态栏

    本文转载至 http://www.cocoachina.com/industry/20131104/7287.html 跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外 ...

  4. 学习tornado

    http://old.sebug.net/paper/books/tornado/ http://demo.pythoner.com/itt2zh/index.html http://tornado- ...

  5. hdu3729(二分图)

    比赛的时候没有想到二分图,一直在想dp和贪心. 原因是因为看到数据是100000所以直接就没有往二分图匹配上想. 现在想想. 因为二分图两边的太不对称了,60 和100000 , 如果用匈牙利算法考虑 ...

  6. 修改yum源为阿里云的

    将Centos的yum源更换为国内的阿里云源 author:headsen chen date:2018-04-28  13:33:41 1.备份  mv /etc/yum.repos.d/CentO ...

  7. 《从零开始学Swift》学习笔记(Day54)——抛出错误

    原创文章,欢迎转载.转载请注明:关东升的博客 能放到try后面调用函数或方法都是有要求的,他们是有可能抛出错误,在这些函数或方法声明的参数后面要加上throws关键字,表示这个函数或方法可以抛出错误. ...

  8. 动态获取selected的value值

    两个select选项,第一个选项可以选1-9之间的数字,第二个选项可以选0到9之间的数字,要满足成人和儿童的总和不大于9,且一个成人最多带两名儿童 <div> <span>成人 ...

  9. 通过创建脚本代替"scrapy crawl Test"命令

    文件结构: 1.在命令行scrapy crawl Test正常运行的条件下在项目的根目录创建start.py文件(注意目录,同scrapy.cfg文件一个目录): start.py源代码: # -*- ...

  10. python之MySQL学习——简单的增删改查封装

    1.增删改查封装类MysqlHelper.py import pymysql as ps class MysqlHelper: def __init__(self, host, user, passw ...