PHP代码优化之include

来源:互联网 发布:魅蓝note6网络不稳定 编辑:程序博客网 时间:2024/04/29 12:21

require

[root@localhost httpd]# strace -p 5064
Process 5064 attached - interrupt to quit
accept(3, {sa_family=AF_INET6, sin6_port=htons(51642), inet_pton(AF_INET6, "::ffff:192.168.124.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [47038481825820]) = 9
fcntl(9, F_GETFD)                       = 0
fcntl(9, F_SETFD, FD_CLOEXEC)           = 0
getsockname(9, {sa_family=AF_INET6, sin6_port=htons(80), inet_pton(AF_INET6, "::ffff:192.168.124.129", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [68719476764]) = 0
fcntl(9, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(9, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
brk(0x1007c000)                         = 0x1007c000
read(9, "GET /require.php HTTP/1.1\r\nHost:"..., 8000) = 378
stat("/phpstudy/www/require.php", {st_mode=S_IFREG|0644, st_size=131, ...}) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={60, 0}}, NULL) = 0
rt_sigaction(SIGPROF, {0x2ac8583e1170, [PROF], SA_RESTORER|SA_RESTART, 0x3239630030}, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [PROF], NULL, 8) = 0
brk(0x1009d000)                         = 0x1009d000
getcwd("/phpstudy/server/httpd/bin"..., 4095) = 27
chdir("/phpstudy/www")                  = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={30, 0}}, NULL) = 0
lstat("/phpstudy/www/require.php", {st_mode=S_IFREG|0644, st_size=131, ...}) = 0
lstat("/phpstudy/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/phpstudy", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0

open("/phpstudy/www/require.php", O_RDONLY) = 10
fstat(10, {st_mode=S_IFREG|0644, st_size=131, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=131, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=131, ...}) = 0
mmap(NULL, 131, PROT_READ, MAP_SHARED, 10, 0) = 0x2ac85fa2c000
munmap(0x2ac85fa2c000, 131)             = 0
close(10)                               = 0
stat("./ClassA.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassA.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassA.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2ac85fa2c000
munmap(0x2ac85fa2c000, 22)              = 0
close(10)                               = 0
stat("./ClassB.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassB.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassB.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2ac85fa2c000
munmap(0x2ac85fa2c000, 22)              = 0
close(10)                               = 0
stat("./ClassC.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassC.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassC.php", O_RDONLY) = 10
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2ac85fa2c000
munmap(0x2ac85fa2c000, 22)              = 0
close(10)                               = 0
stat("./ClassD.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassD.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassD.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2ac85fa2c000
munmap(0x2ac85fa2c000, 22)              = 0
close(10)                               = 0
chdir("/phpstudy/server/httpd/bin")     = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
read(9, 0x10059878, 8000)               = -1 EAGAIN (Resource temporarily unavailable)
writev(9, [{"HTTP/1.1 200 OK\r\nDate: Fri, 25 S"..., 223}, {"\n\n\n\nOnly testing require.", 25}], 2) = 248
write(7, "192.168.124.1 - - [24/Sep/2015:2"..., 82) = 82
times({tms_utime=1, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 429433883
poll([{fd=9, events=POLLIN}], 1, 5000)  = 0 (Timeout)
shutdown(9, 1 /* send */)               = 0
poll([{fd=9, events=POLLIN}], 1, 2000)  = 0 (Timeout)
close(9)                                = 0
read(5, 0x7ffff94a3d47, 1)              = -1 EAGAIN (Resource temporarily unavailable)
accept(3, {sa_family=AF_INET6, sin6_port=htons(51643), inet_pton(AF_INET6, "::ffff:192.168.124.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [47038481825820]) = 9
fcntl(9, F_GETFD)                       = 0
fcntl(9, F_SETFD, FD_CLOEXEC)           = 0
getsockname(9, {sa_family=AF_INET6, sin6_port=htons(80), inet_pton(AF_INET6, "::ffff:192.168.124.129", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [68719476764]) = 0
fcntl(9, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(9, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
read(9, "", 8000)                       = 0
poll([{fd=9, events=POLLIN}], 1, 20000) = 1 ([{fd=9, revents=POLLIN}])
shutdown(9, 1 /* send */)               = 0
poll([{fd=9, events=POLLIN}], 1, 2000)  = 1 ([{fd=9, revents=POLLIN|POLLHUP}])
read(9, "", 512)                        = 0
close(9)                                = 0
read(5, 0x7ffff94a3d47, 1)              = -1 EAGAIN (Resource temporarily unavailable)

require_once:

[root@localhost bin]# strace -p 5077
Process 5077 attached - interrupt to quit
accept(3, {sa_family=AF_INET6, sin6_port=htons(51986), inet_pton(AF_INET6, "::ffff:192.168.124.1", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [47502338293788]) = 9
fcntl(9, F_GETFD)                       = 0
fcntl(9, F_SETFD, FD_CLOEXEC)           = 0
getsockname(9, {sa_family=AF_INET6, sin6_port=htons(80), inet_pton(AF_INET6, "::ffff:192.168.124.129", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [68719476764]) = 0
fcntl(9, F_GETFL)                       = 0x2 (flags O_RDWR)
fcntl(9, F_SETFL, O_RDWR|O_NONBLOCK)    = 0
brk(0x1453a000)                         = 0x1453a000
read(9, "GET /require_once.php HTTP/1.1\r\n"..., 8000) = 357
stat("/phpstudy/www/require_once.php", {st_mode=S_IFREG|0644, st_size=155, ...}) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={60, 0}}, NULL) = 0
rt_sigaction(SIGPROF, {0x2b3422aa2170, [PROF], SA_RESTORER|SA_RESTART, 0x3239630030}, {SIG_DFL, [], 0}, 8) = 0
rt_sigprocmask(SIG_UNBLOCK, [PROF], NULL, 8) = 0
brk(0x1455b000)                         = 0x1455b000
getcwd("/phpstudy/server/httpd/bin"..., 4095) = 27
chdir("/phpstudy/www")                  = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={30, 0}}, NULL) = 0
lstat("/phpstudy/www/require_once.php", {st_mode=S_IFREG|0644, st_size=155, ...}) = 0
lstat("/phpstudy/www", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
lstat("/phpstudy", {st_mode=S_IFDIR|0755, st_size=4096, ...}) = 0
open("/phpstudy/www/require_once.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=155, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=155, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=155, ...}) = 0
mmap(NULL, 155, PROT_READ, MAP_SHARED, 10, 0) = 0x2b342a0ed000
munmap(0x2b342a0ed000, 155)             = 0
close(10)                               = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassA.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
lstat("/phpstudy/www/ClassA.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassA.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2b342a0ed000
stat("/phpstudy/www/ClassA.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
munmap(0x2b342a0ed000, 22)              = 0
close(10)                               = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassB.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
lstat("/phpstudy/www/ClassB.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassB.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2b342a0ed000
stat("/phpstudy/www/ClassB.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
munmap(0x2b342a0ed000, 22)              = 0
close(10)                               = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassC.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
lstat("/phpstudy/www/ClassC.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassC.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2b342a0ed000
stat("/phpstudy/www/ClassC.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
munmap(0x2b342a0ed000, 22)              = 0
close(10)                               = 0
getcwd("/phpstudy/www"..., 4096)        = 14
lstat("/phpstudy/www/./ClassD.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
lstat("/phpstudy/www/ClassD.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
open("/phpstudy/www/ClassD.php", O_RDONLY) = 10

fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
fstat(10, {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
mmap(NULL, 22, PROT_READ, MAP_SHARED, 10, 0) = 0x2b342a0ed000
stat("/phpstudy/www/ClassD.php", {st_mode=S_IFREG|0644, st_size=22, ...}) = 0
munmap(0x2b342a0ed000, 22)              = 0
close(10)                               = 0
chdir("/phpstudy/server/httpd/bin")     = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
setitimer(ITIMER_PROF, {it_interval={0, 0}, it_value={0, 0}}, NULL) = 0
read(9, 0x14517878, 8000)               = -1 EAGAIN (Resource temporarily unavailable)
writev(9, [{"HTTP/1.1 200 OK\r\nDate: Fri, 25 S"..., 223}, {"\n\n\n\nOnly testing require_once", 29}], 2) = 252
write(7, "192.168.124.1 - - [24/Sep/2015:2"..., 87) = 87
times({tms_utime=2, tms_stime=2, tms_cutime=0, tms_cstime=0}) = 429504658
poll([{fd=9, events=POLLIN}], 1, 5000)  = 0 (Timeout)
shutdown(9, 1 /* send */)               = 0
poll([{fd=9, events=POLLIN}], 1, 2000)  = 1 ([{fd=9, revents=POLLIN|POLLHUP}])
read(9, "", 512)                        = 0
close(9)                                = 0
read(5, 0x7fff41717607, 1)              = -1 EAGAIN (Resource temporarily unavailable)

require_once比require多一次lstat(),所以require在一般情况下快于require。

include_once与include的对比也是如此。


经过压力测试结果得出:

1.如果是单个文件简单测试include,require性能高(但实际项目中多次加载性能就会缺失);

2.如果是在实际项目中还是要使用include_once,require_once,只需要加载一次便可;


参考:

http://blog.163.com/023_dns/blog/static/11872736620127192447736/

深入理解php原理之include include_once require require_once  

作者写的很不错,可见其功底,新手勿看。





0 0
原创粉丝点击