require和require_once经济性能对比
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经济性能对比的更多相关文章
- 记录:sea.js和require.js配置 与 性能对比
最近有点忙,很久无写博客,记录一下之前的配置require.js和sea.js的配置.(有误有望提出 require.js 文件目录 /app(项目使用js) /lib(require.js jq存放 ...
- PHP函数include include_once require和require_once的区别
了解下include.include_once.require和require_once这4个函数: include函数:会将指定的文件读入并且执行里面的程序: require函数:会将目标文件的内容 ...
- [原] KVM 环境下MySQL性能对比
KVM 环境下MySQL性能对比 标签(空格分隔): Cloud2.0 [TOC] 测试目的 对比MySQL在物理机和KVM环境下性能情况 压测标准 压测遵循单一变量原则,所有的对比都是只改变一个变量 ...
- 浅谈C++之冒泡排序、希尔排序、快速排序、插入排序、堆排序、基数排序性能对比分析之后续补充说明(有图有真相)
如果你觉得我的有些话有点唐突,你不理解可以想看看前一篇<C++之冒泡排序.希尔排序.快速排序.插入排序.堆排序.基数排序性能对比分析>. 这几天闲着没事就写了一篇<C++之冒泡排序. ...
- Java--Stream,NIO ByteBuffer,NIO MappedByteBuffer性能对比
目前Java中最IO有多种文件读取的方法,本文章对比Stream,NIO ByteBuffer,NIO MappedByteBuffer的性能,让我们知道到底怎么能写出性能高的文件读取代码. pack ...
- C正则库做DNS域名验证时的性能对比
C正则库做DNS域名验证时的性能对比 本文对C的正则库regex和pcre在做域名验证的场景下做评测. 验证DNS域名的正则表达式为: "^[0-9a-zA-Z_-]+(\\.[0-9a ...
- [转]require(),include(),require_once()和include_once()区别
require(),include(),require_once()和include_once()区别 面试中最容易提到的一个PHP的问题,我想和大家共勉一下: require()和include() ...
- 开发语言性能对比,C++、Java、Python、LUA、TCC
一直想做开发语言性能对比,刚好有时间都做了给大家参考一下, 编译类:C++和Java表现还不错 脚本类:TCC脚本动态运行C语言,性能比其他脚本快好多... 想玩TCC的同学下载测试包,TCC目录下修 ...
- php+mysql预查询prepare 与普通查询的性能对比
prepare可以解决大访问量的网站给数据库服务器所带来的负载和开销,本文章通过实例向大家介绍预查询prepare与普通查询的性能对比,需要的朋友可以参考一下. 实例代码如下: <?php cl ...
随机推荐
- Multiview
新建3个ViewController的类 新建main.stroyboard,并配置好相应的布局(编辑界面,连接视图与类,ViewController的Storyboard ID,连接3个Button ...
- C++ 检查Windows服务运行状态
检查Windows服务运行状态 C++ Code 1234567891011121314151617181920212223242526272829303132333435363738394041 ...
- TypeScript 接口(三)
TypeScript的核心原则之一是对值所具有的结构进行类型检查. 接口初始: interface objProperty { name: string } function printName(na ...
- 在UI线程之外,多线程处理Bitmaps
多线程处理Bitmaps 上一篇,我们讨论了:Android有效的处理Bitmap,降低内存 ,可是最好不要运行在主线程(UI线程),假设图片是本地的或者网络的又或者是其它地方的. 图片载入的 ...
- MVC [Control与View交互]
<1> Home控制器 using System; using System.Collections.Generic; using System.Data; using System.Da ...
- iOS 保存异常日志
// // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...
- linux禁用触摸板驱动
Method 1: 终端输入如下命令: sudo modprobe -r psmouse 如果打开触摸板就是: sudo modprobe psmouse ------- Method 2: 第一步: ...
- Powershell数据处理
1.导出csv文档 Export-Csv D:\ps\xxx.csv -Encoding UTF8 -NoTypeInformation 2.发送mail $from="frommailad ...
- convention over configuration 约定优于配置 按约定编程 约定大于配置 PEP 20 -- The Zen of Python
为什么说 Java 程序员必须掌握 Spring Boot ?_知识库_博客园 https://kb.cnblogs.com/page/606682/ 为什么说 Java 程序员必须掌握 Spring ...
- Java 语言基础之数组应用
什么时候使用数组呢? 如果数据出现了对应关系, 而且对应关系的一方是有序的数字编号, 并作为角标使用. 这时,就必须要想到数组的使用. 也就是将这些数据存储到数组中, 根据运算的结果作为角标, 直接去 ...