Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
I dont know java but it looks like a foreach loop where
retval will get the values of the array returned by split -
Snob20866yYep 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
-
Depending on how it’s optimized under the hood, as the amount of splits increases the performance decreases.
-
@bkwilliams I don't think splitting a string is that performance sensitive though.
-
@bkwilliams I wanna say no to that given that String Methods have been around since forever
-
@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. -
@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. -
@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
-
@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. -
@bittersweet dont use map because its syntax is less clear then a regular for loop.
-
@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...) -
@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
-
@bittersweet the slow logic came from you can’t break out from the loop🤔 so it’s always worse O(n).
-
@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? -
@bittersweet i just call split in JS. For basic stuff, keep it short and simple should be top priority
-
musician6656y@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.
Related Rants
what kind of For loop is this?
question
java