1. If a method can be static, declare it static. Speed improvement is by a factor of 4.
¸Þ½îµå°¡ staticÀÌ µÉ ¼ö ÀÖ´Ù¸é staticÀ¸·Î ¼±¾ðÇ϶ó. 4¹è »¡¶óÁø´Ù.
2. echo is faster than print.
echo°¡ printº¸´Ù ºü¸£´Ù.
7. require_once() is expensive
require_once()´Â ºñ½Î´Ù.
8. Use full paths in includes and requires, less time spent on resolving the OS paths.
include¿Í require¸¦ »ç¿ëÇÒ ¶§, °æ·Î¸¦ ã´Âµ¥ ½Ã°£ÀÌ Àû°Ô °É¸®´Â full path¸¦ »ç¿ëÇ϶ó.
10. See if you can use strncasecmp, strpbrk and stripos instead of regex
Á¤±ÔÇ¥Çö½Äº¸´Ù´Â °¡´ÉÇϸé strncasecmp³ª strpbrk, stripos¸¦ »ç¿ëÇ϶ó.
* ¿ªÁÖ
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º¸´Ù 4¹è ºü¸£´Ù.
16. Close your database connections when you¡¯re done with them
DB¸¦ ´Ù »ç¿ëÇßÀ¸¸é ¿¬°áÀ» ´Ý¾Æ¶ó.
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.
°´Ã¼ÀÇ ¸â¹öº¯¼ö¸¦ Áõ°¡½ÃÅ°´Â °ÍÀÌ Áö¿ª º¯¼ö¸¦ Áõ°¡½ÃÅ°´Â °Íº¸´Ù 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°¡ ÀÌÁß µû¿ÈÇ¥ ¾ÈÀÇ º¯¼ö¸¦ ã¾Æº¸Áö¸¸ ´ÜÀÏ µû¿ÈÇ¥ ¾È¿¡¼´Â º¯¼ö¸¦ ãÁö ¾Ê±â ¶§¹®ÀÌ´Ù. ¹°·Ð ¹®ÀÚ¿ ¾È¿¡¼ º¯¼ö¸¦ °¡Áú ÇÊ¿ä°¡ ¾øÀ» ¶§¸¸ ÀÌ·¸°Ô »ç¿ëÇÒ ¼ö ÀÖ´Ù.
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´Â °í¼º´É ¸Þ¸ð¸® °´Ã¼ ij½Ì ½Ã½ºÅÛÀÌ´Ù.
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()ÀÌ ¾ð¾î ±âº»¹®¹ýÀÌ°í ÇÔ¼ö°¡ ¾Æ´Ï±â ¶§¹®¿¡ ÇÔ¼ö ã¿Í lowercase ÀÛ¾÷À» ÇÊ¿ä·Î ÇÏÁö ¾ÊÀ¸¹Ç·Î strlen()º¸´Ù ´õ ºü¸¦ ¼öµµ ÀÖ´Ù. ÀÌ°ÍÀº °¡»óÀûÀ¸·Î ¹®ÀÚ¿ÀÇ ±æÀ̸¦ °áÁ¤ÇÏ´Â ½ÇÁ¦ Äڵ忡 °úºÎÇÏ°¡ ¾ø´Ù´Â °ÍÀ» ÀǹÌÇÑ´Ù.
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 a 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´Â PHP¿¡¼ Á» ´õ ºü¸¥µ¥ ±×°ÍÀº $i++¿¡ 4°³ÀÇ opcode°¡ »ç¿ëµÇ´Â ´ë½Å¿¡ 3°³¸¸ ÇÊ¿äÇϱ⠶§¹®ÀÌ´Ù. ÈÄÁõ°¡´Â »ç½Ç Áõ°¡µÉ Àӽú¯¼öÀÇ »ý¼ºÀ» ÃÊ·¡ÇÑ´Ù. ¹Ý¸é¿¡ ÀüÁõ°¡´Â ¿ø·¡ °ªÀ» Á÷Á¢ Áõ°¡½ÃŲ´Ù. ÀÌ°ÍÀº opcode°¡ ZendÀÇ PHP optimizeró·³ ÃÖÀûÈÇÏ´Â ÃÖÀûÈ ±â¹ýÀÇ ÇϳªÀÌ´Ù. ¸ðµç opcode optimizerµéÀÌ ÀÌ ÃÖÀûȸ¦ ¼öÇàÇÏ´Â °ÍÀº ¾Æ´Ï°í ¸¹Àº ISP¿Í serverµéÀÌ opcode optimizer¾øÀÌ ¼öÇàµÇ°í Àֱ⠶§¹®¿¡ ¸í½ÉÇÏ´Â °Ô ÁÁÀ» °ÍÀÌ´Ù.
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
¼ö¸¹Àº ¹Ì¸® Á¤ÀÇµÈ ÇÔ¼ö¸¦ È°¿ëÇضó.
40. If you have very time consuming functions in your code, consider writing them as C extensions
¸Å¿ì ½Ã°£À» ¼ÒºñÇÏ´Â ÇÔ¼ö°¡ ÀÖ´Ù¸é, C È®ÀåÀ¸·Î ÀÛ¼ºÇÏ´Â °ÍÀ» °í·ÁÇغÁ¶ó.