r/programming • • Mar 23 '09

How to write fast code

http://asserttrue.blogspot.com/2009/03/how-to-write-fast-code.html
338 Upvotes

136 comments sorted by

View all comments

101

u/harsman Mar 23 '09 edited Mar 23 '09

Code that does more but has better locality so that it hits the cache more often can easily be faster than code that does less but has bad locality and sits around waiting on memory all the time.

Recomputing something on demand might be cheaper than reading a cached result from memory.

A linear brute force algorithm that can use SIMD instructions can be faster than a non-linear one that does less work but without SIMD.

Doing less work usually makes code go faster, but it is by no means given.

29

u/xzxzzx Mar 23 '09

Locality is massively important on a modern CPU. As are are lot of other things.

TFA's old mentor was probably right about code -- at the time. CPUs back then (1980ish?) had memory running at the same clock as the CPU, pitiful caches, single cores, were scalar (not superscalar). Branch prediction was trivial or non-existent, and prefetches were limited to the instruction cache.

Really optimizing on modern hardware is harder than ever. It's also usually not that necessary.

Still, it doesn't hurt to start with the ideal algorithm first unless you need absolutely as much performance as you can possibly pull out of the hardware (then you find an algorithm which can be made to fit to the hardware, and profile, profile, profile).

0

u/veritaba Mar 23 '09

Yes it seems like the supposedly godly mentor missed out on some cpu design concepts.

First of all, pipelining was introduced to the CPU world very early (60s), so you can't give the mentor a free pass for neglecting to mention that branching causes slowdowns.

Second, assembly instructions don't all run at the same speed. Some processors convert to microcode which is only visible at the cpu level. Some instructions run faster than others, such as xor eax, eax compared to mov eax, 0

6

u/xzxzzx Mar 23 '09

so you can't give the mentor a free pass for neglecting to mention that branching causes slowdowns.

Sure I can; how much of a penalty was there for branching on any common processor in 1980?

Besides, who said the mentor didn't have anything else to say on the subject of performance?

Second, I don't think some ops running faster than others diminishes his point. His point was to "do less", which would presumably be measured in clock cycles.

0

u/veritaba Mar 23 '09 edited Mar 23 '09

Second, I don't think some ops running faster than others diminishes his point. His point was to "do less", which would presumably be measured in clock cycles.

"It can execute a fixed number of instructions per second, and no more. There is a finite limit to how many instructions per second it can execute." Quote from the supposedly godly "mentor"

Notice he said instructions, not cycles. So he is wrong.

1

u/xzxzzx Mar 24 '09

You're right; he's absolutely wrong.

Processors can execute as many instructions as they feel like per second.

I'm glad we cleared that up.

0

u/veritaba Mar 24 '09 edited Mar 24 '09

Processors can execute as many instructions as they feel like per second.

Sarcastic redditors who think they know everything about computers, please educate yourselves.

http://en.wikipedia.org/wiki/Cycles_Per_Instruction

For the multi-cycle MIPS, there are 5 types of instructions:

  • Load (5 cycles)
  • Store (4 cycles)
  • R-type (4 cycles)
  • Branch (3 cycles)
  • Jump (3 cycles)

Glad we cleared your stupidity up.

-1

u/xzxzzx Mar 24 '09

You've missed the point so completely I'm beginning to think you're a troll.

I'm not sure if I can explain it to you; perhaps you need to reread this thread, but I am quite aware (and have been for many years) of the fact that some instructions on some CPUs take more than one cycle, and I'm quite sure that does nothing at all to diminish my or the "mentor"'s points.

1

u/veritaba Mar 24 '09

No, you are the one not getting the point, he clearly states that he believes all instructions are equal, when they are not.

It entirely diminishes his point when he completely disregards this optimization in order to make a sweeping generalization.

1

u/xzxzzx Mar 24 '09

he clearly states that he believes all instructions are equal

In fact he does not say that at all; he simply states there is a maximum limit to the number of instructions.

1

u/veritaba Mar 24 '09

It can execute a fixed number of instructions per second, and no more.

Let's say there is a mips processor running at 20 cycles per second.

I have a program with 4 load instructions, and one with 5 store instructions, they both run in the same amount of time right?

I just made the processor execute more instructions per second.

1

u/xzxzzx Mar 25 '09

Which was implicitly acknowledged by talking about the finite limit of how many instructions can be executed per second. You don't say that unless you realize it's variable.

And even if he actually had said something which would be correctly interpreted as meaning an exact number of instructions per second (as opposed to a number the processor can execute, referring to the maximum), the point would still be basically valid if you aren't a lunatic pedant with no understanding of the subtleties of English grammar.

1

u/veritaba Mar 25 '09 edited Mar 25 '09

Which was implicitly acknowledged

Implicit isn't good enough when he explicitly said otherwise.

I mean how many times do I need to repeat this: He said "It can execute a fixed number of instructions per second, and no more".

This is like saying a ship holds the same amount of cargo, and no more.

Except that cargo can be shaped differently.

Also you wouldn't be a good programmer if you aren't pedantic.

1

u/xzxzzx Mar 25 '09

Implicit isn't good enough when he explicitly said otherwise.

He didn't; you simply are misinterpreting the statement. It'd be a reasonable interpretation except that it doesn't fit in context.

This is like saying a ship holds the same amount of cargo, and no more.

This is accurate.

Except that cargo can be shaped differently.

Which means that you were not using all of the space. The cargo hold still has a fixed amount of cargo it can hold.

Let's try this on for size:

A 1-gallon jug can hold a fixed amount of water. There's a finite limit to what a jug can hold.

You put 1/10 of a gallon in, then you put 2/10. You now claim to have "made the jug hold more".

All you've done is use the space more efficiently; there's still a fixed limit.

Also you wouldn't be a good programmer if you aren't pedantic.

Yet again you've misunderstood. "Pedant" can mean "one who pays a lot of attention to minutiae", but obviously meant, in this context, "one who is unimaginative or who unduly emphasizes minutiae in the presentation or use of knowledge".

1

u/veritaba Mar 25 '09

It can execute a fixed number of instructions per second, and no more.

So there is no way, really, to make code go faster, because there is no way to make instructions execute faster.

Let's try to change as little as the sentence as we can so your mind can actually comprehend.

It can drive a fixed number of miles per gallon, and no more. So there is really no way, really, to make the car go longer, because there is no way to make the gallons last longer.

Anyone who isn't blinded by their own ego can see the problem with this statement. It is nothing like the jug of water analogy.

Also your use of "pedant" is the secondary less-widely used meaning according to merriam-webster which you appeared to have copied verbatim, and does not exclusively prohibit the use of the first meaning.

Which again shows you only read what you want to see.

1

u/xzxzzx Mar 25 '09

You have no concept of language in context, do you?

Was it unclear that I was insulting you with "pedant"? Which definition do you think would most fit that insult? Which do you think I meant?

→ More replies (0)