仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 909|回复: 18
打印 上一主题 下一主题

[学习教程] PHP网页编程之优化PHP法式的办法

[复制链接]
老尸 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-2-3 23:32:57 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
怎么培养啊 别光说不练啊,好 ,比如新人入门自己步是配置环境,虽然现在都有很多的集成环境,但是真实的体验下配置环境还是会有很多帮助,不论是你以后工作还是在真实的linux下开发。      1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 假如一个办法可静态化,就对它做静态声明。速度可提拔至4倍。
    2. echo is faster than print. echo 比 print 快。
    3. Use echo's multiple parameters instead of string concatenation. 利用echo的多重参数(译注:指用逗号而不是句点)取代字符串联接。
    4. Set the maxvalue for your for-loops before and not in the loop. 在履行for轮回之前肯定最大轮回数,不要每轮回一次都盘算最大值。
    5. Unset your variables to free memory, especially large arrays. 刊出那些不必的变量特别是大数组,以便释放内存。
    6. Avoid magic like __get, __set, __autoload 尽可能防止利用__get,__set,__autoload.
    7. require_once() is expensive require_once()价值昂贵。
    8. Use full paths in includes and requires, less time spent on resolving the OS paths. 在包括文件时利用完全途径,解析操作体系途径所需的工夫会更少。
    9. If you need to find out the time when the script started executing, $_SERVER['REQUEST_TIME'] is preferred to time() 假如你想晓得剧本入手下手履行(译注:即办事器端收到客户端恳求)的时辰,利用$_SERVER['REQUEST_TIME']要好过time()。
    10. See if you can use strncasecmp, strpbrk and stripos instead of regex. 反省是不是能用strncasecmp,strpbrk,stripos函数取代正则表达式完成不异功效。
    11. str_replace is faster than preg_replace, but strtr is faster than str_replace by a factor of 4. str_replace函数比preg_replace函数快,但strtr函数的效力是str_replace函数的四倍。
    12. If the function, such as string replacement function, accepts both arrays and single characters as arguments, and if your argument list is not too long, consider writing a few redundant replacement statements, passing one character at a time, instead of one line of code that accepts arrays as search and replace arguments. 假如一个字符串交换函数,可承受数组或字符作为参数,而且参数长度不太长,那末可以思索额定写一段交换代码,使得每次传递参数是一个字符,而不是只写一行代码承受数组作为查询和交换的参数。
    13. It's better to use select statements than multi if, else if, statements. 利用选择分支语句(译注:即switch case)好过利用多个if,else if语句。
    14. Error suppression with @ is very slow. 用@屏障毛病动静的做法十分低效。
    15. Turn on apache's mod_deflate 翻开apache的mod_deflate模块。
    16. Close your database connections when you're done with them. 数据库毗连当利用终了时应关失落。
    17. $row['id'] is 7 times faster than $row[id]. $row['id']的效力是$row[id]的7倍。
    18. Error messages are expensive. 毛病动静价值昂贵。
    19. Do not use functions inside of for loop, such as for ($x=0; $x < count($array); $x) The count() function gets called each time. 尽可能不要在for轮回中利用函数,好比for ($x=0; $x < count($array); $x)每轮回一次城市挪用count()函数。
    20. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function. 在办法中递增部分变量,速度是最快的。几近与在函数中挪用部分变量的速度相当。
    21. Incrementing a global variable is 2 times slow than a local var. 递增一个全局变量要比递增一个部分变量慢2倍。
    22. Incrementing an object property (eg. $this->prop++) is 3 times slower than a local variable. 递增一个对象属性(如:$this->prop++)要比递增一个部分变量慢3倍。
    23. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one. 递增一个未预界说的部分变量要比递增一个预界说的部分变量慢9至10倍。
    24. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var)。 PHP probably does a check to see if the global exists. 仅界说一个部分变量而没在函数中挪用它,一样会减慢速度(其水平相当于递增一个部分变量)。PHP也许会反省看是不是存在全局变量。
    25. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance. 办法挪用看来与类中界说的办法的数目有关,由于我(在测试办法之前和以后都)添加了10个办法,但功能上没有变更。
    26. Methods in derived classes run faster than ones defined in the base class. 派生类中的办法运转起来要快于在基类中界说的一样的办法。
   
    27. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations. 挪用带有一个参数的空函数,其消费的工夫相当于履行7至8次的部分变量递增操作。相似的办法挪用所消费的工夫接近于15次的部分变量递增操作。
    28. Surrounding your string by ' instead of " will make things interpret a little faster since php looks for variables inside "…" but not inside '…'. Of course you can only do this when you don't need to have variables in the string. 用单引号取代双引号来包括字符串,如许做会更快一些。由于PHP会在双引号包抄的字符串中搜索变量,单引号则不会。固然,只要当你不需求在字符串中包括变量时才可以这么做。
    29. When echoing strings it's faster to separate them by comma instead of dot. Note: This only works with echo, which is a function that can take several strings as arguments. 输入多个字符串时,用逗号取代句点来分隔字符串,速度更快。注重:只要echo能这么做,它是一种可以把多个字符串看成参数的"函数"(译注:PHP手册中说echo是言语布局,不是真实的函数,故把函数加上了双引号)。
    30. A PHP script will be served at least 2-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. Apache解析一个PHP剧本的工夫要比解析一个静态HTML页面慢2至10倍。尽可能多用静态HTML页面,罕用剧本。
    31. Your PHP scripts are recompiled every time unless the scripts are cached. Install a PHP caching product to typically increase performance by 25-100% by removing compile times. 除非剧本可以缓存,不然每次挪用时城市从头编译一次。引入一套PHP缓存机制凡是可以提拔25%至100%的功能,以避免除编译开支。
    32. Cache as much as possible. Use memcached - memcached is a high-performance memory object caching system intended to speed up dynamic web applications by alleviating database load. OP code caches are useful so that your script does not have to be compiled on every request. 尽可能做缓存,可以使用memcached.memcached是一款高功能的内存对象缓存体系,可用来减速静态Web使用法式,加重数据库负载。对运算码(OP code)的缓存很有效,使得剧本不用为每一个恳求做从头编译。
    33. When working with strings and you need to check that the string is either of a certain length you'd understandably would want to use the strlen() function. This function is pretty quick since it's operation does not perform any calculation but merely return the already known length of a string available in the zval structure (internal C struct used to store variables in PHP)。 However because strlen() is a function it is still somewhat slow because the function call requires several operations such as lowercase & hashtable lookup followed by the execution of said function. In some instance you can improve the speed of your code by using an isset() trick. 当操作字符串并需求查验其长度是不是知足某种请求时,你想固然地会利用strlen()函数。此函数履行起来相当快,由于它不做任何盘算,只前往在zval布局(C的内置数据布局,用于存储PHP变量)中存储的已知字符串长度。然而,因为strlen()是函数,多几何少会有些慢,由于函数挪用会经由诸多步调,如字母小写化(译注:指函数名小写化,PHP不辨别函数名巨细写)、哈希查找,会跟从被挪用的函数一同履行。在某些情形下,你可使用isset()技能减速履行你的代码。
    Ex.(举例以下)
    if (strlen($foo) < 5) { echo "Foo is too short"; }
    vs.(与上面的技能做对照)
    if (!isset($foo{5})) { echo "Foo is too short"; }
    Calling isset() happens to be faster then strlen() because unlike strlen(), isset() is a language construct and not a function meaning that it's execution does not require function lookups and lowercase. This means you have virtually no overhead on top of the actual code that determines the string's length. 挪用isset()刚巧比strlen()快,由于与后者分歧的是,isset()作为一种言语布局,意味着它的履行不需求函数查找和字母小写化。也就是说,实践上在查验字符串长度的顶层代码中你没有花太多开支。
    34. When incrementing or decrementing the value of the variable $i++ happens to be a tad slower then ++$i. This is something PHP specific and does not apply to other languages, so don't go modifying your C or Java code thinking it'll suddenly become faster, it won't. ++$i happens to be faster in PHP because instead of 4 opcodes used for $i++ you only need 3. Post incrementation actually causes in the creation of a temporary var that is then incremented. While pre-incrementation increases the original value directly. This is one of the optimization that opcode optimized like Zend's PHP optimizer. It is still a good idea to keep in mind since not all opcode optimizers perform this optimization and there are plenty of ISPs and servers running without an opcode optimizer. 当履行变量$i的递增或递加时,$i++会比++$i慢一些。这类差别是PHP独有的,其实不合用于其他言语,所以请不要修正你的C或Java代码并期望它们能当即变快,没用的。++$i更快是由于它只需求3条指令(opcodes),$i++则需求4条指令。后置递增实践上会发生一个一时变量,这个一时变量随后被递增。而前置递增直接在原值上递增。这是最优化处置的一种,正如Zend的PHP优化器所作的那样。切记这个优化处置不掉为一个好主张,由于并非一切的指令优化器城市做一样的优化处置,而且存在大批没有拆卸指令优化器的互联网办事供应商(ISPs)和办事器。
    35. Not everything has to be OOP, often it is too much overhead, each method and object call consumes a lot of memory. 并非事必面向对象(OOP),面向对象常常开支很大,每一个办法和对象挪用城市损耗良多内存。
    36. Do not implement every data structure as a class, arrays are useful, too. 并不是要用类完成一切的数据布局,数组也很有效。
    37. Don't split methods too much, think, which code you will really re-use. 不要把办法细分得过量,细心想一想你真正盘算重用的是哪些代码?
    38. You can always split the code of a method later, when needed. 当你需求时,你总能把代码分化成办法。
    39. Make use of the countless predefined functions. 尽可能采取大批的PHP内置函数。
    40. If you have very time consuming functions in your code, consider writing them as C extensions. 假如在代码中存在大批耗时的函数,你可以思索用C扩大的体例完成它们。
    41. Profile your code. A profiler shows you, which parts of your code consumes how many time. The Xdebug debugger already contains a profiler. Profiling shows you the bottlenecks in overview. 评价查验(profile)你的代码。查验器会告知你,代码的哪些局部损耗了几何工夫。Xdebug调试器包括了查验法式,评价查验整体上可以显示出代码的瓶颈。
    42. mod_gzip which is available as an Apache module compresses your data on the fly and can reduce the data to transfer up to 80%. mod_zip可作为Apache模块,用来即时紧缩你的数据,并可以让数据传输量下降80%.
怎样学习,大家都知道编程是1门很枯燥的事业,所以大家一定要有兴趣,可能刚开始打算学的时候是因为别人说php有多好,php多么流行,但是后来伴随着学习的深入,你的这些
再见西城 该用户已被删除
沙发
发表于 2015-2-4 02:02:03 | 只看该作者
Apache不是非得用80或者8080端口的,我刚开始安得时候就是80端口老占用,就用了个 81端口,结果照常,就是输localhost的时候,应该输入为 localhost:81
蒙在股里 该用户已被删除
板凳
发表于 2015-2-9 05:49:42 | 只看该作者
爱上php,他也会爱上你。
因胸联盟 该用户已被删除
地板
发表于 2015-2-10 23:49:22 | 只看该作者
这些都是最基本最常用功能,我们这些菜鸟在系统学习后,可以先对这些功能深入研究。
只想知道 该用户已被删除
5#
发表于 2015-2-12 08:46:23 | 只看该作者
首推的搜索引擎当然是Google大神,其次我比较喜欢 百度知道。不过搜出来的结果往往都是 大家copy来copy去的,运气的的概率很大。
冷月葬花魂 该用户已被删除
6#
发表于 2015-3-2 20:20:39 | 只看该作者
小鸟是第一次发帖(我习惯潜水的(*^__^*) 嘻嘻……),有错误之处还请大家批评指正,另外,前些日子听人说有高手能用php写驱动程序,真是学无止境,人外有人,天外有天。
金色的骷髅 该用户已被删除
7#
发表于 2015-3-7 11:59:29 | 只看该作者
其实也不算什么什么心得,在各位大侠算是小巫见大巫了吧,望大家不要见笑,若其中有错误的地方请各位大虾斧正。
小魔女 该用户已被删除
8#
发表于 2015-3-13 00:08:30 | 只看该作者
你很难利用原理去编写自己的代码。对于php来说,系统的学习我认为还是很重要的,当你有一定理解后,你可你针对某种效果研究,我想那时你不会只是复制代码的水平了。
小妖女 该用户已被删除
9#
发表于 2015-3-17 22:08:08 | 只看该作者
最后祝愿,php会给你带来快乐的同时 你也会给他带来快乐。
简单生活 该用户已被删除
10#
发表于 2015-3-18 23:19:30 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
飘飘悠悠 该用户已被删除
11#
发表于 2015-3-24 10:53:10 | 只看该作者
对于初学者来说不推荐去拿钱买的。当然如果一个网站你经常去用,而且里面的资料也比较有用,最好还是买个会员比较好,毕竟那些也是别人的工作成果。
兰色精灵 该用户已被删除
12#
发表于 2015-4-5 12:26:08 | 只看该作者
不禁又想起那些说php是草根语言的人,为什么认得差距这么大呢。
老尸 该用户已被删除
13#
 楼主| 发表于 2015-4-7 15:27:39 | 只看该作者
写js我最烦的就是 ie 和 firefox下同样的代码 结果显示的结果千差万别,还是就是最好不要用遨游去调试,因为有时候遨游是禁用js的,有可能代码是争取结果被遨游折腾的认为是代码写错。
爱飞 该用户已被删除
14#
发表于 2015-4-16 07:10:29 | 只看该作者
至于模板嘛,各位高人一直以来就是争论不休,我一只小菜鸟就不加入战团啦,咱们新手还是多学点东西的好。
深爱那片海 该用户已被删除
15#
发表于 2015-4-26 07:09:16 | 只看该作者
为了以后维护的方便最好是代码上都加上注释,“予人方便,自己方便”。此外开发文档什么的最好都弄齐全。我觉得这是程序员必备的素质。虽然会消耗点很多的时间。但是确实是非常有必要的。
不帅 该用户已被删除
16#
发表于 2015-4-30 19:48:46 | 只看该作者
实践是检验自己会不会的真理。
灵魂腐蚀 该用户已被删除
17#
发表于 2015-6-18 01:58:16 | 只看该作者
要进行开发,搭建环境是首先需要做的事,windows下面我习惯把环境那个安装在C盘下面,因为我配的环境经常出现诡异事件,什么事都没做环境有的时候就不能用啦。
若相依 该用户已被删除
18#
发表于 2015-6-27 06:13:34 | 只看该作者
微软最近出的新字体“微软雅黑”,虽然是挺漂亮的,不过firefox  支持的不是很好,所以能少用还是少用的好。
第二个灵魂 该用户已被删除
19#
发表于 2015-7-5 01:04:50 | 只看该作者
刚开始安装php的时候,我图了个省事,把php的扩展全都打开啦(就是把php.ini 那一片 extension 前面的冒号全去掉啦),这样自然有好处,以后不用再需要什么功能再来打开。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-5-13 08:27

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表