|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
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多么流行,但是后来伴随着学习的深入,你的这些 |
|