PHPの効率的な書き方及び原因(転載)
13248 ワード
1. :
, , 1/4, , 。
, , 。
: , , , , , , 。
, , , , 。 , , , 。
, , 。
2.echo print, echo ,print ;
:
Echo
0.000929 - 0.001255 s ( 0.001092 seconds)
Print
0.000980 - 0.001396 seconds ( 0.001188 seconds)
8% , echo 。
,echo , 。 apached mod_deflate ob_start 。
3. , ;
。
4. , ;
php , php zend ,
,PHP 1/10, , C 100M , PHP 1G。
PHP , 。
5. __get, __set, __autoload ;
__ , 。 ,
__construct(),__destruct(),__get(),__set(),__unset(),__call(),__callStatic(),__sleep(),__wakeup(),__toString(),__set_state(),__clone(),__autoload()
, __autoload ( , , ) , ( include path ) , I/O , I/O , autoload 。
, , 。 ,autoload 。
:autoload , autoload, .
__autoload , 。
6.requiere_once() ;
requiere_once ), 。 require/include 。
7. includes requires 。
,PHP include_path 。
, 。
8. ,$_SERVER['REQUSET_TIME'] time();
。 , 。
9. PHP , , ; ;
, 。
? :strpbrk()strncasecmp()strpos()/strrpos()/stripos()/strripos() strtr ,
strtr:
'e', )); // bad
?>
:10 。
10.str_replace preg_replace , strtr str_replace 1/4;
,str_replace 。 ! :
strpos ( ), , , :- : , 0.1% 。
: strpos 200%。
11.
, , , , , 。 ,1+1>2;
12. @, @ ;
@ 。 @ @, :3 。 @, 5 , error_reporting(0) , , @ 。
13.$row['id'] $row[id] 7
;
14.
For($x=0; $x < count($array); $x), count() ; 。
16. , ;
17. 2 ;
, , cache, CPU 。
, , ,cpu cache , 。
( )
18. ( ) ($this->prop++) 3 ;
19. 9-10
20. ( )。
PHP ;
21.
10 ( ) ;
22. ;
23. 7-8 $localvar++ , ( ) 15 $localvar++ ;
24 , 。
PHP , 。
PHP , ! PHP , , 。 , 。
。
BAD:
$output = "This is a plain string";
GOOD:
$output = 'This is a plain string';
BAD:
$type = "mixed";
$output = "This is a $type string";
GOOD:
$type = 'mixed';
$output = 'This is a ' . $type .' string';
25. echo 。
echo “ ”( :PHP echo , , )。
echo $str1,$str2。
26.Apache PHP HTML 2 10 。
HTML , 。
28. , memcached。
, , ;
(OP code) , 。
29. ip2long() long2ip() IP 。
1/4 。 ;
30. checkdnsrr() email
IP ;
31. mysql_* mysqli_*;
32. (?:);
33. PEAR
, PEAR 。PEAR , php ;
35. error_reporting(0) 。
php.ini 。 ,php.ini , error_reporting(0) , (
require_once() ) SQL ;
36. gzcompress() gzuncompress() ( ) ( ) 。
gzip 90%;
37. 。
“&” ;
38. SQL 。
Fully understand “magic quotes” and the dangers of SQL injection. I’m hoping that most developers reading this are already familiar with SQL injection. However, I list it here because it’s absolutely critical to understand. If you’ve never heard the term before, spend the entire rest of the day googling and reading.
39. isset strlen
, strlen() 。 , , zval (C , PHP ) 。 , strlen() , , , ( : ,PHP )、 , 。 , isset() 。
( )
if (strlen($foo) < 5) { echo “Foo is too short”$$ }
( )
if (!isset($foo{5})) { echo “Foo is too short”$$ }
isset() strlen() , ,isset() , 。 , 。
40. ++$i
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 preincrementation 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 3 (opcodes),$i++ 4 。 , 。 。 , Zend PHP 。 , ,
(ISPs) 。
40.
PHP , PHP ( ) , , 。 , , 512KB , 1MB !
BAD:
$description = $_POST['description'];
echo $description;
GOOD:
echo $_POST['description'];
41
switch case if,else if , 。
42. file_get_contents file、fopen、feof、fgets
file_get_contents file、fopen、feof、fgets , file_get_contents, ! file_get_contents URL PHP ;
43. , PHP ;
44. Select SQL , Insert、Update ( update , );
45. PHP
46. , :
( PHP ?);
47. ;
48.foreach , foreach while for ;
49.“ i+=1 i=i+1。 c/c++ , ”;
50. global , unset() ;
51 (OOP), , 。
52 , ?
53 , C 。
54、 apache mod_deflate , 。
( echo )
55、 , 。
56、split exploade
split()
0.001813 - 0.002271 seconds (avg 0.002042 seconds)
explode()
0.001678 - 0.003626 seconds (avg 0.002652 seconds)
Split can take regular expressions as delimiters, and runs faster too. ~23% on average.
1. , 。 4 。
2.echo print 。
3. echo ( : ) 。
4. for , 。
5. , 。
6. __get,__set,__autoload。
7.require_once() 。
8. , 。
9. ( : ) , $_SERVER[‘REQUEST_TIME’] time()。
10. 。
11.str_replace preg_replace , strtr str_replace 。
12. , , , , , 。
13. ( : switch case) if,else if 。
14. @ 。
15. apache mod_deflate 。
16. 。
17.$row[‘id’] $row[id] 7 。
18. 。
19. for , for ($x=0; $x < count($array); $x) count() 。
20. , 。 。
21. 2 。
22. ( :$this->prop++) 3 。
23. 9 10 。
24. , ( )。PHP 。
25. , ( ) 10 , 。
26. 。
27. , 7 8 。 15 。
28. , 。 PHP , 。 , 。
29. , , 。 : echo , “ ”( :PHP echo , , )。
30.Apache PHP HTML 2 10 。 HTML , 。
31. , 。 PHP 25% 100% , 。
32. , memcached。memcached , Web , 。 (OP code) , 。
33. , strlen() 。 , , zval (C , PHP ) 。 , strlen() , , , ( : ,PHP )、 , 。 , isset() 。
( )
if (strlen($foo) < 5) { echo "Foo is too short"$$ }
( )
if (!isset($foo{5})) { echo "Foo is too short"$$ }
isset() strlen() , ,isset() , 。 , 。
34. $i ,$i++ ++$i 。 PHP , , C Java , 。++$i 3 (opcodes),$i++ 4 。 , 。 。 , Zend PHP 。 , , (ISPs) 。
35. (OOP), , 。
36. , 。
37. , ?
38. , 。
39. PHP 。
40. , C 。
41. (profile) 。 , 。Xdebug , 。
42.mod_zip Apache , , 80%。
, 。 PHP , , : echo , “ ”( :PHP echo , , )。
static, static, 4 。
$row[’id’] $row[id] 7 。
echo print , echo ( : ) , echo $str1,$str2。
for , , foreach 。
, 。
__get,__set,__autoload。
require_once() ,include , PHP include_path , 。
( : ) , $_SERVER[‘REQUEST_TIME’] time()。
。
str_replace preg_replace , strtr str_replace 。
, , , , , 。
( : switch case) if,else if 。
@ , 。
apache mod_deflate , 。
, 。
。
, . . 2 . ( :$this->prop++) 3 . 9 10 。
, ( )。 PHP 。
, ( ) 10 , 。
。
, 7 8 。 15 。
Apache PHP HTML 2 10 。 HTML , , , 。 PHP 25% 100% , 。 , memcached。 memcached , Web , 。 (OP code) , 。
, strlen() 。 , , zval (C , PHP ) 。 , strlen() , , , ( : ,PHP )、 , 。 , isset() 。 isset() strlen() , ,isset() , 。 , 。
$i ,$i++ ++$i 。 PHP , , C Java , 。++$i 3 (opcodes),$i++ 4 。 , 。 。 , Zend PHP 。 , , (ISPs) 。
(OOP), , 。
, 。
, ? , 。
PHP 。
, C 。
(profile) 。 , 。 Xdebug , 。
mod_zip Apache , , 80%。
file_get_contents file、fopen、feof、fgets , file_get_contents, ! file_get_contents URL PHP ;
, PHP ;
Select SQL , Insert、Update ;
** , : ( PHP ?);
;
PHP , ;
foreach , foreach while for ;
“ i+=1 i=i+1。 c/c++ , ”;
global , unset() ;