suchen
Heimphp教程php手册php中inlcude()性能对比详解
php中inlcude()性能对比详解Jun 13, 2016 am 11:58 AM
includephp代码复制对比性能详解

include性能

复制代码 代码如下:


include('include.php');



当然这种方式并没有错误,只不过在效率上它比下面的方式要稍稍差些:

复制代码 代码如下:


include(realpath(dirname(_FILE_)).DIRECTORY_SEPARATOR.'include.php');



这种方式我们可能需要输入更多一些,但相对于前面那种需要PHP引擎去include_path 中迭代查找所有名称为'include.php'才能查找到相应对象来说,dirname(__FILE__)这种绝对路径的指定会让系统迅速定位到相应文件。

在PHP中常量__FILE__ 其实跟C#中的AppDomain.CurrentDomain.BaseDirectory非常类似,它返回的是当前代码正在执行的代码所在文件的绝对路径。而函数dirname()则返回其父文件夹路径。
另外一个更查找效率高,并且书写简单的方式是include('./include.php'),这相当于告诉系统查找当前路径下的'include.php'文件。

在大型系统中我们常常使用另外一个更好的方式,我们常常在路由文件或其他初始化文件中加入如下代码:

复制代码 代码如下:


define('APP_PATH',realpath(dirname(_FILE_)));


这相当于给系统添加了一个全局变量来指出系统根目录,当我们后面需要引用某具体路径下的文件时我们就可以使用如下代码了:

复制代码 代码如下:


include(APP_PATH.DIRECTORY_SEPARATOR.'models'.'User.php');



autoload与include性能比较

例如有以下四个脚本:

复制代码 代码如下:


#file:include1.php
include 'include2.php';
//@todo something#file:include2.php
//@todo something#file:script1.php
include 'include2.php';
//@todo something
#file:script2.php
include 'include1.php';
include 'script1.php'
//@todo something


当执行script1.php时, include ‘include2.php'; 这行代码被执行了一次。而执行script2.php时,这行代码被执行了两次。
这里只是一个简单的例子,在实际的项目中,include2.php被include的次数可能更多。这样反复的include,是否会影响性能呢?为此我写了个脚本来测试。

复制代码 代码如下:


#file:SimpleClass.php
class SimpleClass {
public function __construct() {
echo get_time() . "rn";
}
}
#file:php_include.php
for($i = 0;$i include_once "SimpleClass.php";
new SimpleClass();
}



当$loop值为1时,脚本耗时约0.00018906593322754秒,当$loop为1000时,脚本耗时约0.076701879501343秒。

如果我们用autoload实现呢?

复制代码 代码如下:


#file:php_autoload.php
function __autoload($class_name) {
include_once $class_name . '.php';
}for($i = 0;$i new SimpleClass();
}


在这段代码中,我定义了__autoload函数,几乎一样的脚本,当$loop为1时,耗时0.0002131462097168秒,而当$loop为1000时,耗时仅为前面代码的1/7,0.012391805648804秒。
但请注意看SimpleClass的代码,其中输出了一行字符串,如果去掉这行输出后再比较,会是什么样的结果呢?

在$loop同为1000的情况下,前者耗时0.057836055755615秒,而使用了autoload后,仅仅0.00199294090271秒!效率相差近30倍!

从上面的测试可以看出,当文件仅仅被include一次,autoload会消耗稍微多一点的时间,但如果在文件被反复include的情况下,使用autoload则能大大提高系统性能。
至于是否要使用autoload来解放程序员,这就仁者见仁,智者见智了。在我看来,条件允许的前提下,牺牲这一点性能(某些情况下,甚至可能是提升性能)更为便捷的开发,是值得的。

include()和require()性能

对include()来说,在 include()执行时文件每次都要进行读取和评估;
而对于require()来说,文件只处理一次(实际上,文件内容替换了require()语句)。
这就意味着如果有包含这些指令之一的代码和可能执行多次的代码,则使用require()效率比较高。
另一方面,如果每次执行代码时相读取不同的文件,或者有通过一组文件叠代的循环,就使用include(),
因为可以给想要包括的文件名设置一个变量,当参数为include()时使用这个变量。
Stellungnahme
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heiße Werkzeuge

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

MinGW – Minimalistisches GNU für Windows

MinGW – Minimalistisches GNU für Windows

Dieses Projekt wird derzeit auf osdn.net/projects/mingw migriert. Sie können uns dort weiterhin folgen. MinGW: Eine native Windows-Portierung der GNU Compiler Collection (GCC), frei verteilbare Importbibliotheken und Header-Dateien zum Erstellen nativer Windows-Anwendungen, einschließlich Erweiterungen der MSVC-Laufzeit zur Unterstützung der C99-Funktionalität. Die gesamte MinGW-Software kann auf 64-Bit-Windows-Plattformen ausgeführt werden.

SAP NetWeaver Server-Adapter für Eclipse

SAP NetWeaver Server-Adapter für Eclipse

Integrieren Sie Eclipse mit dem SAP NetWeaver-Anwendungsserver.

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

mPDF

mPDF

mPDF ist eine PHP-Bibliothek, die PDF-Dateien aus UTF-8-codiertem HTML generieren kann. Der ursprüngliche Autor, Ian Back, hat mPDF geschrieben, um PDF-Dateien „on the fly“ von seiner Website auszugeben und verschiedene Sprachen zu verarbeiten. Es ist langsamer und erzeugt bei der Verwendung von Unicode-Schriftarten größere Dateien als Originalskripte wie HTML2FPDF, unterstützt aber CSS-Stile usw. und verfügt über viele Verbesserungen. Unterstützt fast alle Sprachen, einschließlich RTL (Arabisch und Hebräisch) und CJK (Chinesisch, Japanisch und Koreanisch). Unterstützt verschachtelte Elemente auf Blockebene (wie P, DIV),