Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts

    Is the time it takes to perform functions negligible?

    I'm currently taking a class on Assembly Language and a strong focus in the class is looking at how long different actions take at the base level to actually perform. For example we've looked at the difference between multiplying variables by a number n as opposed to just adding a number n times. Another example would be incrementing a number instead of adding 1 to a number.

    My question is, does PHP behave this way as well? Should I focus on how long different operations/functions will take, such as incrementation vs addition and multiplication vs addition. Thanks!

    PS: Feel free to apply this to higher level php functions as well.

  • #2
    Senior Coder
    Join Date
    Aug 2006
    Posts
    1,269
    Thanks
    10
    Thanked 277 Times in 276 Posts
    All programming will behave this way, but the issue you'll have is that it's more difficult to know what a higher level language is really doing to affect timing. For example a PHP array is a very odd beast, and if you were going to do some timing on various array functions, you'd have quite a hard time characterizing it, because it's not done in an obvious way. While I'd say you should be concerned about performance, I personally would not even consider the option of using multiple adds instead of a single multiply in a higher level language. The concept of understanding the issues is good, but should be applied to larger "heavy" algorithms (sorting, database normalization, etc).

    I applaud your class and learnings, as they will help you write better code regardless of the language.

    Dave

  • #3
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by tracknut View Post
    a PHP array is a very odd beast.
    I was actually wondering about arrays as well. When I was learning about arrays I was surprised that php allows you to simply add a value to an array. This went against everything I've ever learned, so I'd guess in reality PHP recreates the array with length++ or something along those lines. Sounds like a lengthy process.

    Anyways, thanks for the insight! Will definitely use it to guide my learning.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Quote Originally Posted by KULP View Post
    Should I focus on how long different operations/functions will take, such as incrementation vs addition and multiplication vs addition.
    The difference in PHP is going to be negligible at that low level unless you are doing a large calculation or doing it inside of a large loop. There are many other things you can do to improve your code performance, but changing the way you perform calculations isn't going to affect performance significantly. You will gain much more of an improvement by using best practices and a caching module such as APC.

  • #5
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Very interesting article! Good list of tips. Most all of them are very simple to do, as well. Neat

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    One difference between higher level languages and assember languages is that the higher level languages have the code fed through a compiler or interpretor which depending on how good it is can apply various optimisations so as to hopefully pick the variant of the machine code that will run faster.

    That PHP gets compiled over again each time someone access the page probably has a greater effect on runtime that anything in the code itself.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by felgall View Post
    One difference between higher level languages and assember languages is that the higher level languages have the code fed through a compiler or interpretor which depending on how good it is can apply various optimisations so as to hopefully pick the variant of the machine code that will run faster.

    That PHP gets compiled over again each time someone access the page probably has a greater effect on runtime that anything in the code itself.
    Yeah I was reading up on interpreted (php, javascript) vs compiled (java, c++, etc) when I pondered the original question. Even more of a reason why what's inside matters so much, right? Something interpreted over and over again vs something compiled just once.

    Would it be smart to have as little reliance on php as possible? For example use html for everything possible and then fill in the blanks. (over simplified)Example:

    Code:
    <html>
    ...
    <p>My name is <?php echo "Kulp"; ?></p>
    ...
    </html>
    as opposed to

    Code:
    <html>
    ...
    <?php echo "<p>My name is Kulp</p>"; ?>
    ...
    </html>
    Note: Speaking optimization explicitly.
    Last edited by KULP; 11-20-2012 at 03:56 AM.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,293
    Thanks
    13
    Thanked 345 Times in 341 Posts
    Quote Originally Posted by KULP View Post
    Would it be smart to have as little reliance on php as possible? For example use html for everything possible and then fill in the blanks. (over simplified)Example:

    Code:
    <html>
    ...
    <p>My name is <?php echo "Kulp"; ?></p>
    ...
    </html>
    as opposed to

    Code:
    <html>
    ...
    <?php echo "<p>My name is Kulp</p>"; ?>
    ...
    </html>
    From a design point it would be smart to separate HTML and PHP and thus use a template.
    PHP Code:
    // just one possibility
    $str = <<<HTML
    <html>
      ...
      <p>My name is %s</p>
      ...
    </html>
    HTML;
    // another possibility
    $str file_get_contents("html_template_1.html");

    // whereever you get that name from
    $name "Kulp";
    // ...
    printf($str$name); 
    as others have already said, micro-optimisation does not cut it. you have to look for the real bottlenecks and also consider design (how effectively you can write the code).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As previously stated, micro optimization is a waste of time as long as good coding practices are observed. PHP code executes very fast. What takes a long time is anything requiring reading from or writing to the hard drive such as file includes and database queries. This is probably the main reason people have problems with Wordpress performance (80+ file includes for every page view with the default theme). A few thousand lines of PHP code can be executed in the same amount of time it takes to do a select query. Minimize file includes and database queries and that will go a long way to improving performance of PHP scripts.

  • #10
    Regular Coder
    Join Date
    Mar 2012
    Posts
    168
    Thanks
    5
    Thanked 11 Times in 11 Posts
    Quote Originally Posted by billzo View Post
    As previously stated, micro optimization is a waste of time as long as good coding practices are observed. PHP code executes very fast. What takes a long time is anything requiring reading from or writing to the hard drive such as file includes and database queries. This is probably the main reason people have problems with Wordpress performance (80+ file includes for every page view with the default theme). A few thousand lines of PHP code can be executed in the same amount of time it takes to do a select query. Minimize file includes and database queries and that will go a long way to improving performance of PHP scripts.
    Ah, had no idea about the drag of includes. Only using 2 (header & footer) on my current project, so I think I'm good there. There is so much more to web programming than people try to make there out to be here in the computer science department.

  • #11
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KULP View Post
    Ah, had no idea about the drag of includes.
    The developers of Wordpress and other CMSes don't seem to, either. If you are on a dedicated box (or maybe even a VPS), you can put an accelerator on it which will cache PHP opcode in memory and it won't have to go to the hard drive for every page view. Most websites are hosted on shared servers where this isn't an option.

    One large PHP file with 20,000 lines of code is going to execute much faster than loading 20 include files with 1,000 lines of code each.

    We also have to keep in mind we are entering the era of solid state hard drives that can access data hundreds of times faster than mechanical drives. While minimizing the number of file includes still remains important, it will probably be less important as time goes on. It will probably just create an excuse for more poor programming practices.
    Last edited by billzo; 11-20-2012 at 06:46 PM.

  • #12
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,640
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by KULP View Post
    Yeah I was reading up on interpreted (php, javascript) vs compiled (java, c++, etc) when I pondered the original question.
    JavaScript is interpreted, PHP is compiled (but when the page is requested, not prior to that the way C++ is). Java is normally only compiled into an intermediate level language that is then interpreted.

    You can tell PHP is compiled because you get ALL the syntax errors immediately when you try to access the page and not one at a time the way you do with an interpreted language.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •