Results 1 to 5 of 5

Thread: foreach fails. faster iterations?

  1. #1

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    foreach fails. faster iterations?

    For discussion;

    I've read up on (and performed my own) benchmarking for lengthy tasks and their faster functional equivalencies in PHP. Often it's said that micro-optimizing code is a worthless task, but I figure; sort them out before deployment to minimize code maintenance if the project grows.

    A common one is foreach vs. various. foreach often fails in efficiency. Now, in my tests (and I've not seen this method much) when you want to iterate straight through an array, the following works the fastest:

    PHP Code:
    $i_ceil count($my_array);
    while(
    $i++ <= $i_ceil){
        
    //process on $my_array[$i]

    Has anyone encountered faster? Has anyone found this approach unfeasible?
    IWS

  2. #2
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    Re: foreach fails. faster iterations?


  3. #3

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: foreach fails. faster iterations?

    Oh I have already, thank you Zach_VB6. In fact, I've considered creating my own site for that purpose, complete user submissions, discussion, and multiple interpreted language categories.

    I actually found in my testing (on many runs) that the while(){} iterator is several hundred times faster than foreach and it's equally slow counterparts. I'll post my brief analysis later this afternoon.
    IWS

  4. #4
    Lively Member
    Join Date
    Sep 2010
    Location
    Canada
    Posts
    68

    Re: foreach fails. faster iterations?

    I wonder if ++$i < $i_ceil is faster than $i++ <= i_ceil?

  5. #5

    Thread Starter
    Hyperactive Member tomcatexodus's Avatar
    Join Date
    Feb 2001
    Posts
    372

    Re: foreach fails. faster iterations?

    Quote Originally Posted by squared View Post
    I wonder if ++$i < $i_ceil is faster than $i++ <= i_ceil?
    Haven't tested, wouldn't think there would be a difference though. They're identical except in timing.
    IWS

Posting Permissions

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



Click Here to Expand Forum to Full Width