Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Adding values in single variable

  1. #11
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by djr33 View Post
    Isn't that 2 tenths of a millisecond?
    umm.... yes.

  2. #12
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,134
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    But only 1000 iterations over an array with only 3 members. Could be an anomaly, unless you ran each test 10 times and got the same result each time. Go 10000 times and make the string longer - like 50 digits. Then the results should be more definitive.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #13
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I did each test about a dozen times; what I posted was the average. There were some outliers. But, all in all, I didn't mean for it to be "definitive" anyway.

    But, ask and ye shall receive: averages for 10,000 iterations, using PHP_INT_MAX (which, on my system (64 bit) was 9,223,372,036,854,775,807):

    My method:
    0.18127489089966 sec
    John's first method:
    0.20594382286072 sec
    John's second method:
    0.23329019546509 sec

  4. The Following User Says Thank You to traq For This Useful Post:

    djr33 (10-30-2013)

  5. #14
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,134
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    I think foreach is faster than while in this case because while has to do a full array lookup each iteration. Using foreach is perhaps somehow more sequential and/or the array is in memory.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #15
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    In my imagination, foreach is a much more natural operation than for/while, but I haven't seen any evidence (until now) to support that. I don't know what kind of logic/math is actually behind it (I had always pessimistically assumed it was just a for loop using count(), roughly), but at least in theory I can imagine a very efficient mechanism that uses the array pointer to make it go faster-- in fact, I think that's exactly what's happening here. The array pointer is increased by one each time. Then by default it'll stop at the end of the array.
    I don't know much about this, but I do know that arrays have points and that, if you do things the wrong way, they can actually mess things up-- like starting in the middle of the array next time, but only if you have very weird (and relatively low level / manual code, functions like reset() and friends).
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #16
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    I guess we're all just going to have to learn c++ and go find out.

  8. #17
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,134
    Thanks
    44
    Thanked 3,231 Times in 3,192 Posts
    Blog Entries
    12

    Default

    I think Daniel and I are onto something. It seems fairly likely that since foreach means that the entire array is to be processed, the array is held in memory and the pointer is advanced incrementally for each iteration. Whereas, while and for loops are arbitrary. Each iteration could potentially involve anything. Just because the first iteration performs a lookup in an array, doesn't necessarily mean that same array will even be involved in the second iteration. So nothing is held in memory.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  9. #18
    Join Date
    Jul 2012
    Posts
    197
    Thanks
    54
    Thanked 3 Times in 3 Posts

    Default

    Quote Originally Posted by djr33 View Post
    By the way, letom, have we answered your question? You now have a few versions to pick from so you can use whatever you want. If you need more help, feel free to ask
    Noted with Thanks..
    You will know the truth, and the truth will set you free (John 8:32)(The truth is about the only begotten son of GOD.....When you feel you are alone for facing your problems, God will never leave you by your own You are important for God

Similar Threads

  1. Replies: 1
    Last Post: 10-27-2010, 06:05 PM
  2. Replies: 3
    Last Post: 05-27-2008, 06:36 PM
  3. Replies: 2
    Last Post: 05-26-2008, 09:16 AM
  4. Replies: 3
    Last Post: 08-03-2007, 11:22 PM
  5. insert dynamic values into javascript variable
    By rich1234 in forum JavaScript
    Replies: 10
    Last Post: 07-29-2007, 11:02 AM

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
  •