SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Does declaring a function add server load

    If I have a simple script like the following where the test.php file is a huge pile of inefficient code which will slow my site to a crawl:
    Code php:
    <?php
    function test() {
    	require('test.php');
    }
    test();

    Would it run slower than the following?
    Code php:
    <?php
    function test() {
    	require('test.php');
    }

    ie: Does the test.php script run when the function is declared, when it is used or both times?

    I'm trying to improve the efficiency of some of my scripts and am wondering if there is any point in adding IF statements so that the functions aren't created unneccessarily.


    Thanks for your advice

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you have a reason to declare it as a function, then any added nanoseconds because of the declaration are more than worth it.

    That is unless you have ALOT of functions, in which case it's best to sort them into files, and only use certain includes when those functions are required.

    Efficiency issues usually come with querying and code which should be in objects/functions.

    The best way to optimise your code is to paste it here. Exceptional things can happen after SilverBullet, crmalibu and I have finished with it!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    If you declare several million functions all in the one script then it would add a millisecond or two to the run time. It would probably save hours if not days though when performing future maintenance on the code in the script due to the better organisation.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks! This is more of a general question rather than a specific case here though ... well, sort of.

    I thought of it while reworking a dropdown menu plugin I made for WordPress. I often hear hardcore coders whining about 'script kiddies' hacking away and creating ridiculously inefficient code to achieve simple tasks so I'm trying to avoid being included in that bunch (the script kiddies I mean). The plugin grabs multiple bits of information from the database on each page reload, however at the moment I'm calling ALL of the possible options whereas in reality they are not all necessary so I figure I may as well avoid that if possible.

    I also figure if I can make a small program run as efficiently as possible, then it's a good lesson in how to build much larger applications.

    The best way to optimise your code is to paste it here. Exceptional things can happen after SilverBullet, crmalibu and I have finished with i
    Thanks for the offer. Once I've got my plugin into a reasonable state (it's a bit of mess at the moment) I'll post the code here for you to tear to pieces for me It's been a while since I've had anything critiqued and it will probably do me some good ... or drive me insane, one of the two

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It's a bit of mess at the moment
    You don't see much of the code us guys rewrite, do you?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To actually answer your question, the require statement only gets executed if the function is called. Calling the function again will execute it again.

  7. #7
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    To actually answer your question, the require statement only gets executed if the function is called. Calling the function again will execute it again.
    That wasn't what I was originally trying to ask, but is a darn handy thing to know as I can use that to avoid having to check if the function is needed or not by just placing it in an include.

  8. #8
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    A big consideration for programming is that while neatly written easy to read code may run slightly less efficiently than a tangled mess, it it the tangled mess that is actually what is looked down upon as that makes maintaining the code much harder as well as the code perhaps being more error prone (since it is harder for people to see exactly what it is doing). The huge savings in maintenance effort more than make up for the slight inefficiencies of using a more structured approach to writing the code in the first place. If that were not the case then no structured forms of programming would exist since any form of structuring adds an overhead to the processing.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, your question was "Does the test.php script run when the function is declared, when it is used or both times?" so it only gets require'd (run) once the function is called/used.

  10. #10
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sk89q View Post
    Well, your question was "Does the test.php script run when the function is declared, when it is used or both times?" so it only gets require'd (run) once the function is called/used.
    I wrote my question wrong and may have misinterpreted your answer.

    Does it matter if it is an include? Or could I just dumpy 10,000 lines of code inside the function instead and it still take as long to load?

  11. #11
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No. Nothing in the function gets evaluated until you actually call the function. It gets parsed, but otherwise it's a waste of space until you do! However, if you don't ever call the function, dumping all the lines of code into the function does use (very slightly) more resources than if you included the code later.

  12. #12
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ryanhellyer View Post
    Does it matter if it is an include? Or could I just dumpy 10,000 lines of code inside the function instead and it still take as long to load?
    If a function comprises 10000 lines of code, then you should really be splitting it down into smaller functions.
    Functions should be small, manageable units of code.
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  13. #13
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    This was more a theoretical question as I definitely don't have 10,000 lines of code to add to a script, at maximum probably 200. This is definitely great to know though as it means my scripts are better coded than I thought

  14. #14
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're trying to optimize performance, it's important to spend your time where it counts. Code profiling can help big time here.

    You can measure execution time of parts of the code by using microtime(). But, much better would be to use xdebug's profiler. It's worth it, I guarantee you will be suprised whats slow and whats not. Read this too http://www.chazzuka.com/blog/?p=163, but only a profiler will show you the lowdown.

  15. #15
    SitePoint Wizard ryanhellyer's Avatar
    Join Date
    Oct 2006
    Location
    New Zealand
    Posts
    2,323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link, there are lots of handy tips in there.

    I'll have a look at xdebug's profiler shortly.


Bookmarks

Posting Permissions

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