Why is it faster to process a sorted array than an unsorted array? (2012)
stackoverflow.com
stackoverflow.com
Edit: The update is not new, either. Just the part that I found interesting. Apologies for any confusion.
Edit: Whoops, I misunderstood. Sorry!
I imagine the 6x factor probably is because the optimizer unfolds the array/loop structure.
edit: sorting the array increases the time by about 4x if you include the sort time, so its not worth it
We know optimizes unfold loops as well as arrays. it doesnt take a profound leap in logic that the optimizer may have unfolded everything and realized certain code was not going to change the state of the program, thus removed it. Maybe it didnt, but then again maybe it did.
It's information, but not worthwhile information.
> It doesnt take a profound leap in logic that the optimizer may have unfolded everything and realized certain code was not going to change the state of the program, thus removed it. Maybe it didnt, but then again maybe it did.
Prove it. This article has had a ton of traffic. If you see the thing that everyone missed, a lot of people will be very impressed with you.
Well, that... and even simple Python VM instructions tend to take dozens of insns to execute.
(Though, of course, the original algorithm can be written in a one-pass branchless fashion anyway, so the point is kinda moot.)
The important thing is to be keenly aware that this is how a processor works. Branch predictors take up a big chunk of silicon on the CPU and keep very complicated histories, and in certain cases the compiler can outsmart some bad code, but in general it's way too easy to make this kind of mistake.
I assume this means "12 cycles delay × 50% instructions can miss × 50% miss rate". But that's not really right; you're comparing against an IPC of 1 sans misprediction, with each instruction latency-1 and all of them dependent on the prior. Less importantly, you're assuming 50% of the instruction are branches, where it's really more like 25% since you have the load and add as well. Your mispredict penalty also seems a tad small.
A not-horrific compiler and a fast CPU should do far better than that, with peak IPC of just under 4 (since theoretical peak is 4 and there's generally a little overhead). Mispredicts linearize the graph, reducing IPC to some fraction, which you can guess is a bit over 1/2, since you average ~2 loops of 4 instructions per mispredict. This means you've got a reduction of slightly less than 8x.
https://github.com/frankmcsherry/blog/blob/master/posts/2015...
It's about how sorting + random access can be faster than random access, because you introduce locality of reference. And in this case, it absolutely is faster to sort the data first and then do the work, even counting the sorting.
Edit: Aw crap, adrianN beat me to this a few screenfuls down, sorry! But, it is a different post, so maybe this is still helpful. :D
It would be interesting to see how Clang/LLVM do...
TL;DR: Chrome uses quick sort and we managed to hit its worst case by pre-ordering the input alphabetically on the server side.
[0] https://stackoverflow.com/questions/46228556/how-is-array-so...
Lesson 3 covers pipelines and lesson 4 covers branches. Milos does a great job of explaining "How It Works" for something that is really a hidden layer under the CPU.
He references a textbook as well "Computer Systems: A Programmer's Perspective (2nd Edition)". Just bought a copy.
Of course, you could make it faster by removing everything under 128 from the array before starting the loop, but it's not really the point here.
But because it's built-in, the compiler might be able to infer more about it, just like it reduces:
printf("x");
to
putchar('x');
https://news.ycombinator.com/item?id=12490893
https://news.ycombinator.com/item?id=14459549
https://news.ycombinator.com/item?id=12272428
I am not against resubmissions, but if it is an older post the year should be in the title.
Cache, baby! Cache!
To fill in the blanks: Computers, CPU, RAM, and other Storage devices are well optimized for sequential reading and writing.
It's very important to understand it so that you can understand why sometimes pipelining is difficult or impossible.
We used the textbook "Computer Organization and Design" by David A Patterson and John L Hennessy. Branch Prediction comes up on page 341. I have it sitting on my desk here at work because it's such a useful book.
On the other hand, maybe this is why my E-mail client has a 300MB memory footprint and my browser pegs the CPU when simply opening a web page.
It sounds a lot more like you don't fully understand what the causes behind these things are and you're ready to arrogantly blame it on developers who dare use javascript without microoptimizing everything.
Your email client has a 300MB memory footprint most likely because of its dependencies and the appropriate amount of work invested into it. If it's a commercial product, the company probably doesn't care enough to spend decades optimizing every single layer of the stack down to the compiled assembly just to sell you something for twenty bucks. If it's an open source product, the devs behind it definitely don't have time to do that but OTOH you're welcome to apply your superior knowledge and show everyone how it's done.
They quite possibly are, but I didn't do a CS degree so I wasn't sure whether it was a standard thing to learn or not (hence "maybe" rather than "definitely").
You're describing a good question for an end-of-year test. Not an interview question for "entry level positions", most of which don't require or use any academic CS knowledge.
I disagree on the original SO question as being a good weed-out question for an entry level position. This is a code optimization that I certainly wouldn't expect a newbie to know.
There are a lot harder micro-benchmarks related to L2 cache size, page faults, compiler optimizations (esp. JVM), pointer deference/indirections and so on.
This interview question is great for three reasons:
1. It identifies that someone is you.
2. It separates the bad-coder you (before 2017.09.15) from the good-coder you (after 2017.09.15). This means that it is immune from generating a false positive on a poor candidate due to time travel.
3. It identifies cultural fit: the person reads the same news articles you do. If you waste time reading random hacker news articles, you're going to want to hire people who do the same. Especially ones who were around and not too busy on exactly 2017.09.15! It easily weeds out people who were on vacation on that date, for example.
What I especially like about this is that it has nothing to do with anyone's code. (After all, anyone who works at a level that low can answer it very easily without having read this stack overflow question, so it's a strictly orthogonal puzzle: it's only hard for people who don't need it!)
You should go ahead and add this to your list of interview questions! In fact, why not make it the only one?
(It also avoids the fuss of having to come up with questions in any way related to the work that a candidate will be doing, which, in case the above sarcastic comment wasn't clear, is what you should actually be doing.)
This type of interview question simply needs to die. An interview question shouldn't be about whether you've seen something that is unrelated to the job. Which is what this is.
(For jobs where it is actually something they need to know, it is a poor question because it's too simple.)
I should add that I personally found the stack overflow question itself and its answers (especially about the Intel compiler) very interesting.
Apart from being useful knowledge for the job, it's a good indicator, showing if the person has worked on such problems before (or alternatively has good knowledge to tackle such problems)
If they don't know it, they're not an expert. But did they claim to be, and how quickly could they learn?
If they do know it, it doesn't mean they're an expert either. It's easy to learn these things just by happening to read about them.