SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Which foreach is faster?

    Which one of the following foreach's is faster? And why?

    PHP Code:
    foreach($files as $file)
    {
             if(
    $file != "index.html")
             { 
                     
    unlink('uploads/cache/' $file);
             }
    }

    foreach(
    $files as $file)
    {
             if(
    $file == "index.html") continue;
             
    unlink('uploads/cache/' $file);    


  2. #2
    Guru in training bronze trophy SoulScratch's Avatar
    Join Date
    Apr 2006
    Location
    Maryland
    Posts
    1,838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would think they're not too far off speed-wise ( and not that you'll notice unless there are 100+ some files ) but you can time this by creating a timestamp before and after the loop and calculating the difference.
    Cross browser css bugs

    Dan Schulz you will be missed

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Neither is faster or worth your time worrying about. Best to stop worrying about micro optimizations and focus on the truly slow parts using profiling. http://www.xdebug.org/
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, thanks for the hint on profiling.

    When both are not too far off speedwise (some xx µs), what's the better syntax?
    Fmpov the second version is much easier to read, because it's a positive comparison operator.
    The chaining of several != or ! gives me (and probably others too) a hard time understanding the code at first glance.
    In general i think, it's better to use positive logical/comparison operators.
    What do you think?

  5. #5
    Web Professional
    Join Date
    Oct 2008
    Location
    London
    Posts
    862
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mr.vain View Post
    What do you think?
    It depends on the context. When you see the continue keyword you immediately see that the condition is crucial for execution of the rest of the iteration, whereas if you enclose the code in brackets you don’t know if any other code is executed even though the condition has not been met, until you scroll down to the bottom.

    PHP Code:
    foreach($files as $file) {
        if(
    $file != "index.html") { // if file is not index.html then delete it
            
    unlink('uploads/cache/' $file);
        }

        
    // something else can happen here
    }

    foreach(
    $files as $file) {
        if (
    $file == "index.html") { // if file is index.html then go to next one
            
    continue;
        }

        
    // nothing else will happen here
        
    unlink('uploads/cache/' $file);

    Notice how it changes how the code reads.

    Always aim for writing code that is easiest to read and makes sense at first glance.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It is a matter of opinion and what you feel is right. I have my own style which is close to the second version of your code. I personally dislike nested or if statements with else at the end. As soon as possible I try to handle negative events that exit out of the function or loop.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I generally do the former, but if it involves using a bunch of nested if statements, I go for the latter.

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some people strictly follow structured programming principals. They won't use things like continue or break or return inside of a loop.

    Personally, I don't follow the principals strictly. I've found in some situations it can make the code more difficult to read and write. My brain doesn't follow so well when I'm reading code with many levels of nesting.


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
  •