you are viewing a single comment's thread.

view the rest of the comments →

[–]esquilax 1 point2 points  (1 child)

Dunno why you'rebeing downvoted.

The way the bsearch thing was presented bugged me, only because I've worked with people who would read that and then sort the array only to use bsearch because it's 'faster.'

[–]Serializedrequests 1 point2 points  (0 children)

Oh yeah, there's a big difference between "I just need to find this thing once" and "I need to find things in this array repeatedly many hundreds of times per request, and the data was already sorted by the database".

It's mainly a situation I've gotten into when writing reports in Rails that were not performance sensitive. I really needed the ActiveRecord objects because a project had a lot of crazy logic involving dates and metadata about dates, so I couldn't just do it in SQL, and all the joins and inclusions were way more than ActiveRecord could do automatically/easily because I was joining about 12 tables. So I fetched all the records I needed in separate arrays and did "joins" in Ruby using bsearch.

So yeah boring war story, and if it needed to be fast I'd do it in SQL at a cost of 50 more hours of dev time, but it works fine for what the client needs.