cheapbag214s
Dołączył: 27 Cze 2013
Posty: 19304
Przeczytał: 0 tematów
Ostrzeżeń: 0/5 Skąd: England
|
Wysłany: Wto 18:39, 20 Sie 2013 Temat postu: iterate over an array except for an element-spun1 |
|
|
iterate over an array except for an element
I'm using Ruby on Rails 3,[link widoczny dla zalogowanych].0,[link widoczny dla zalogowanych].7 and I would like to iterate over an array of objects (class istances) aside from the element with id equal to 1 (the id refers to the array[1] index).
I understand that I can use an if statement "internally" for an each statement and doing that I look for each "current"\"considered" element if id == 1. However, because the array is populated of the lot of data, I would like to find another way in order to accomplish the same things inside a more performant way (avoiding to run the if each time).
Is IMHO a better way to do the selection rather than using your own explicit if statement. These results actually surprised me, reject is by far the slowest option,[link widoczny dla zalogowanych], followed by the ranges. The greatest performing after not taking out the element whatsoever was using first and drop to pick all of the elements around it.
The outcomes like a percentage using no selection as a baseline:
with if 146%
with first and drop 104%
without if 100%
Obviously this really is highly dependant on that which you do with the elements,[link widoczny dla zalogowanych], this was testing with probably the fastest operation Ruby is capable of doing. The slower the operation the less difference these may have. Of course: Benchmark, Benchmark,[link widoczny dla zalogowanych], Benchmark
Testing an actual for loop may be well worth the 5 minutes it might take. It might be frowned on in Ruby circles, but that doesn't mean it is not ever worth using. When you call each or map or whatever, those methods use for loops in whatever way. Avoid absolutes.
Additionally, it depends how large the array can get, at some n,[link widoczny dla zalogowanych], one might become faster compared to other. In this instance, it's really not worthwhile.
If you do not require a specific element,[link widoczny dla zalogowanych], maybe you don't need to store that row of data inside a database. What's the distinction between row 1 and also the remaining rows,[link widoczny dla zalogowanych], in other words, the reason for skipping it? Is the row with id=1 always going to achieve the exactly the same data inside it? If that's the case, storing it as a constant would probably be better,[link widoczny dla zalogowanych], and will make your question moot. Performance more often than not is more expensive memory.
Unless Rails 3 does things differently, and you pull out the data and employ id because the finder key, id=1 come in element 0.
Unfortunately,[link widoczny dla zalogowanych], Knuth's quote gets misinterpreted a lot and obtain accustomed to excuse crappy, inefficient code that would 't be written when the programmer was sufficiently educated, and considered it for 5 seconds. Sure, spending per week trying to speed up code you don't know is a problem or perhaps is a small problem but this really is more what Knuth was talking about. Performance is one of the most misunderstood and abused concepts in computer science.
相关的主题文章:
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
http://www.drshogun.fora.pl/propozycje,10/louis-vuitton-cheap-shoes-le-process-based-on-ones,7832.html#8222
http://www.luntacunt.fora.pl/rules,3/5-guthrie-tickets-spun4,9176.html#9472
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
http://www.semper.fora.pl/public-questions-to-our-ally,1/best-online-holiday-shopping-spun4,2509.html#4181
http://www.ragaming.fora.pl/home,3/hakka-cuisine-dancing-dragons-heaven-spun3,8049.html#8311
[link widoczny dla zalogowanych]
[link widoczny dla zalogowanych]
http://www.portfolk.fora.pl/o-wszystkim-i-o-niczym,4/margaret-thatcher-s-suits-sell-for-ten-times-aucti,3038.html#3504
Post został pochwalony 0 razy
|
|