2

what kind of For loop is this?

Comments
  • 9
    I dont know java but it looks like a foreach loop where

    retval will get the values of the array returned by split
  • 1
    @Codex404 for-each loop hmm first time heard about that btw thanks
  • 2
    Yep that's a foreach loop... Instead of iterating over something by index within a normal forloop, you iterate over Objects that implement the Interface iterable with the "for(iterator : iterable)" notation
  • 1
    Depending on how it’s optimized under the hood, as the amount of splits increases the performance decreases.
  • 0
    @Codex404 correct answer
  • 0
    @bkwilliams I don't think splitting a string is that performance sensitive though.
  • 0
    @bkwilliams I wanna say no to that given that String Methods have been around since forever
  • 2
    @bittersweet @beggarboy
    if it splits it once and then saves that object in memory with the index, that would be true.

    It could also save all indexes of the split character and remember the index of the indexes and it wouldn’t be that bad.

    If it has to “resplit” on each iteration of the for loop, that would be expensive.

    Again, it depends on how it is optimized under the hood.
  • 2
    @bkwilliams I think it's executed as:

    1. Create an (unnamed) array/list to store the result of the split.

    2. Search through the string up to the each delimiter, put each part in the list.

    3. loop over the resulting list, initializing retval each time to the current item.

    So that's 2 loops, one to split the string, one to loop through the array.

    Compared to many "Collection" classes & methods commonly used for merging/diffing/sorting/filtering/reducing large sets of objects... the performance of a small loop is rarely crucial.

    Performance in most applications is usually bottle necked by I/O anyway, things like repetitive/rate-limited API calls inside loops, fetching full objects from a DB through an ORM, etc.
  • 1
    @bkwilliams @bittersweet 😮 you guys can start a long conversation with for loop! That’s amazing 👍 JS,Python,C++,C# all have for each loop... I guess it‘s standard in very language
  • 1
    @sunfishcc

    I think it's funny how many devs these days say things like: "You shouldn't use array.map(), it's slow compared to a for loop!" -- and then go on to advocate for a microservices architecture for example... where all the performance tends to depend on inter-service latencies.

    I think the best way to approach performance is by real measurement, either directly from production, or preferably using configurable simulations. And then for fun, run the simulations with 10 or 100x the amount of users/documents/articles/products/etc you currently handle, so you can project which bottlenecks need to be optimized in the next few months.
  • 0
    @bittersweet dont use map because its syntax is less clear then a regular for loop.
  • 1
    @Codex404 😮 You take that back, go wash your mouth with some functors, applicatives and monads.

    (or read something like this: https://medium.freecodecamp.org/dis...)
  • 1
    @bittersweet i use almost all of them except the map... I dont see much use for it either since you have foreach for that too
  • 1
    @Codex404 OK so you are at least 90% enlightened 😁
  • 0
    @bittersweet the slow logic came from you can’t break out from the loop🤔 so it’s always worse O(n).
  • 2
    @sunfishcc True -- but whether any complexity analysis matters depends on the the possible size of n. I don't care if something is O(log n) or O(n^3), if n is guaranteed to be small.

    Consider that split function: If it is splitting a single serial key which is formatted like 1234-ABCD-5678, would I really need to care about optimization?
  • 0
    @bittersweet i just call split in JS. For basic stuff, keep it short and simple should be top priority
  • 0
    @Codex404
    Foreach and map are pretty different.
    Map returns a new array with the same amount of values transformed. I dont see how its harder to read than a foreach. Once you know the concept its (imo) even easier since you know that it always returns a new array and usually doesnt modify stuff it shouldn't. With foreach you always have to check inside the function to see what it does.
  • 0
    @musician that was not the point.
Add Comment