Comparison of static and non-static methods in php-php Tutorial

Source: Internet
Author: User
Comparison of static and non-static methods in php

If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly four times.

Practice is the only criterion for testing code execution efficiency. so I took a small test when I got home from work:

Test the code first (if there is something wrong, please correct it ):

Test environment:

/test # php -vPHP 5.4.24 (cli) (built: Jan 19 2014 21:32:15) Copyright (c) 1997-2013 The PHP GroupZend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies

/test # uname -aDarwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0: Thu Jan 16 19:40:37 PST 2014; root:xnu-2422.90.20~2/RELEASE_X86_64 x86_64

System configuration can be viewed in this article: http://blog.csdn.net/lamp_water/article/details/20968155

 test();}echo (microtime(true) - $start) ."\n";$start = microtime(true);for($i=0;$i<10000000;$i++) {    staticTest::testStatic();}echo microtime(true) - $start;

The result of the 10 million execution in a loop is as follows:

/Test #Php staticTest. php

2.2938342094421

1.1871800422668
Result of the second execution:

2.303295135498

1.1807670593262

After the execution is performed many times, it is near 2.3 and 1.18. It can be seen that although the efficiency improvement mentioned in the article is not so terrible, the performance improvement is indeed doubled.

Therefore, we recommend that you:

If the class method can be defined as static, it should be defined as static as much as possible, and its speed will be increased by nearly doubled.

There are still a lot of classes and methods in a normal php Framework. if we can make static methods, it is better to use static methods and the efficiency is relatively high.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.