Static and non-static methods in PHP efficiency comparison

Source: Internet
Author: User
Tags php framework
Static and non-static methods in PHP efficiency comparison

Today, when I read this article, http://jingyan.baidu.com/article/6b97984d7892d21ca2b0bfc2.html found that the first sentence was this.

If you can define a method of a class as static, try to define it as static, and it will rise nearly 4 times times faster.

Practice is the only standard to test the efficiency of code execution, so while coming home from work a little time, did a small test:

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

Test environment:

/test # php-vphp 5.4.24 (CLI) (Built:jan 21:32:15) Copyright (c) 1997-2013 the PHP groupzend Engine v2.4.0, Copy Right (c) 1998-2013 Zend Technologies

/test # Uname-adarwin 192.168.0.101 13.1.0 Darwin Kernel Version 13.1.0:thu Jan 19:40:37 PST 2014; Root:xnu-2422.90.20~2/release_x86_64 x86_64

System configuration can be seen 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 results of the 10 million execution loop execution are as follows:

/test # PHP statictest.php

2.2938342094421

1.1871800422668
Second execution Result:

2.303295135498

1.1807670593262

Executed many times later, all around 2.3 and 1.18. Visible, although no article said the efficiency of the increase is so scary, the performance of the improvement aspect did increase by about one times.

It is therefore recommended that:

If you can define a method of a class as static, try to define it as static, and it will rise nearly 1 time times faster.

For a normal PHP framework, the classes and methods inside are still very large, if we can make static method, or use static method is better, efficiency is higher.





  • 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.