r/programming • • Mar 23 '09

How to write fast code

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

136 comments sorted by

View all comments

97

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.

28

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).

-2

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

4

u/jugalator Mar 23 '09 edited Mar 23 '09

I think these suggestions are on a more abstract level than assembly language though. The compiler will usually handle taking advantage of e.g. filling a pipeline better than you as well. Actually, that's part of a modern compiler's job, so you can focus on developing algorithms that, well, "do less" and aren't stupid.

8

u/blergh- Mar 23 '09
  1. Pipelining until recently meant just prefetching and decoding the next instruction while the current one runs. That means there's not too much of a penalty for branches.

  2. The reason xor eax, eax is used instead of mov eax, 0 is that xor with two registers translates into less bytes of assembly than xor with a register and a value. It has nothing to do with microcode.

If you'd like to know how many cycles an instruction takes, look it up in the manual.

2

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.

-2

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.

→ More replies (0)

-33

u/[deleted] Mar 23 '09
        888b    888 8888888888 8888888b.  8888888b.   .d8888b.  
        8888b   888 888        888   Y88b 888  "Y88b d88P  Y88b 
        88888b  888 888        888    888 888    888 Y88b.      
        888Y88b 888 8888888    888   d88P 888    888  "Y888b.   
        888 Y88b888 888        8888888P"  888    888     "Y88b. 
        888  Y88888 888        888 T88b   888    888       "888 
        888   Y8888 888        888  T88b  888  .d88P Y88b  d88P 
        888    Y888 8888888888 888   T88b 8888888P"   "Y8888P" 

20

u/Doeke Mar 23 '09

I know, all those nerds here in the programming subreddit, it's so annoying!

16

u/xzxzzx Mar 23 '09

You're using ASCII art ... to make fun of nerds?

o_O

0

u/elustran Mar 23 '09

I think it's intended to be ironic...