News for hackers. Stuff that matters.
Re:Optimizations on Linear Search (2016) by jonstewart on Saturday October 03, 2020 @11:26AM (#24672631)
This is such a mashup of conflicting advice. Don’t optimize! Use SIMD! If you’re reading from disk you’re I/O bound! I am embarrassed for the ACM.
Re:Optimizations on Linear Search (2016) by hhmc on Saturday October 03, 2020 @11:43AM (#24672751)
It’s also strange to advocate for using a stdlib provided sort. It’s hard to imagine a stdlib worth it’s salt that provides ‘sort’ but not some kind of ‘linear search’ (and the stdlib implementation would likely use SIMD).
Re:Optimizations on Linear Search (2016) by Joker_vD on Saturday October 03, 2020 @11:59AM (#24672850)
Well, Go's standard library has no linear search routine. But it does have both sort and binary search.
Re:Optimizations on Linear Search (2016) by gameswithgo on Saturday October 03, 2020 @12:21PM (#24673000)
standard libraries are only just now starting to use SIMD at all. .NET has been working hard on SIMDifying their core libraries lately. Do any C++ implementations of the std do so? Or do they just lean on llvm/gcc to autovectorize when possible?
Re:Optimizations on Linear Search (2016) by formerly_proven on Saturday October 03, 2020 @12:26PM (#24673024)
There's really no point providing a hand-rolled SIMD implementation for stuff like std::max_element. It's a very trivial loop that any compiler can and will vectorize.

Auto-vectorization in general "just works" for basic stuff. It's only the complex stuff where you can really benefit from hand-rolled SIMD implementations; you'll know it when you see it.

Re:Optimizations on Linear Search (2016) by kevinventullo on Saturday October 03, 2020 @11:48AM (#24672783)
It doesn’t inspire much confidence in a candidate’s ability to write software of even moderate complexity if their reasoning for using sort instead of a 5-liner was that their 5-liner could contain bugs.
Re:Optimizations on Linear Search (2016) by JackFr on Saturday October 03, 2020 @12:35PM (#24673081)
I sometimes ask "In the language of your choice write a function to find the maximum of a list of integers" as an interview question. I often have to typically follow it by "Really, there's no subtle trick, just find the max...". The number of people who use a 'sort' and take the first element is definitely non-zero. It's also astonishing the number of buggy implementations there are. (Well in fairness there is one buggy implementation which everyone who gets it wrong typically makes -- initially setting the max to 0)

I'm not sure if this is an argument for or against coding exercises in interviews -- it was just a surprising experience for me.

Re:Optimizations on Linear Search (2016) by CyberDildonics on Saturday October 03, 2020 @01:39PM (#24673507)
You have never had anyone initialize the max to MIN_INTEGER or something like that? I have made similar mistakes, but I've had to write these functions before, so I know better.
Re:Optimizations on Linear Search (2016) by lixtra on Saturday October 03, 2020 @02:27PM (#24673798)
I couldn’t remember such a thing as MIN_INTEGER and what if someone changes the data type?

Why not assume the first element is the max and start your search from the second. Ah, dam. Now you have to check that your vector has at least one element.

It ain’t easy.

Re:Optimizations on Linear Search (2016) by kevinventullo on Sunday October 04, 2020 @09:04AM (#24678737)
> Now you have to check that your vector has at least one element.

Which may lead the candidate to ask the interviewer “What should be the behavior on an empty vector?” Returning MIN_INT or throwing an exception may both be reasonable depending on the application, but I would consider it good signal if the candidate at least acknowledges that this is an edge case.

Re:Optimizations on Linear Search (2016) by CyberDildonics on Saturday October 03, 2020 @05:49PM (#24674926)
It is pretty easy, I would expect that at least some people would be able to do it. MIN_INTEGER was an example, it should be obvious you want the minimum for whatever type you are using. Standard C++ has std::numeric_limits<T>::lowest
Re:Optimizations on Linear Search (2016) by lixtra on Saturday October 03, 2020 @06:45PM (#24675233)
Standard C++ also has std::max_element [1] (and accidentally the possible implementation looks surprisingly familiar). Not that I know much of C++.

[1] https://en.cppreference.com/w/cpp/algorithm/max_element

Re:Optimizations on Linear Search (2016) by CyberDildonics on Sunday October 04, 2020 @11:22AM (#24679668)
That returns an iterator the max element, not the value. I'm not sure which part makes this so difficult that everyone fails on the first try.
Re:Optimizations on Linear Search (2016) by jancsika on Saturday October 03, 2020 @07:02PM (#24675314)
> I sometimes ask "In the language of your choice write a function to find the maximum of a list of integers" as an interview question.

And you don't find it important at all to specify whether the code will be in the hot path or not?

Let me ask you-- real quick question here-- how much time would you say you spend each week dealing with these TPS reports? :)

Re:Optimizations on Linear Search (2016) by benrbray on Saturday October 03, 2020 @11:57AM (#24672836)
Writing software of moderate complexity requires delegation. One big way to save dev+testing+debug time is to rely on specialized libraries whose priority is to be fast+correct. If profiling later reveals that sort() is the bottleneck, by all means, optimize away.
Re:Optimizations on Linear Search (2016) by ben-schaaf on Saturday October 03, 2020 @12:30PM (#24673046)
Even worse when you consider most languages have a built-in way to do linear search too, so really it's just turning a fast 1 liner into a more complex slower 1 liner.