Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write fast (low level) code? [closed]

I would like to learn more about low level code optimization, and how to take advantage of the underlying machine architecture. I am looking for good pointers on where to read about this topic.

More details:

I am interested in optimization in the context of scientific computing (which is a lot of number crunching but not only) in low level languages such as C/C++. I am in particular interested in optimization methods that are not obvious unless one has a good understanding of how the machine works (which I don't---yet).

For example, it's clear that a better algorithm is faster, without knowing anything about the machine it's run on. It's not at all obvious that it matters if one loops through the columns or the rows of a matrix first. (It's better to loop through the matrix so that elements that are stored at adjacent locations are read successively.)

Basic advice on the topic or pointers to articles are most welcome.

Answers

Got answers with lots of great pointers, a lot more than I'll ever have time to read. Here's a list of all of them:

  • The software optimization cookbook from Intel (book)
  • What every programmer should know about memory (pdf book)
  • Write Great Code, Volume 2: Thinking Low-Level, Writing High-Level (book)
  • Software optimization resources by Agner Fog (five detailed pdf manuals)

I'll need a bit of skim time to decide which one to use (not having time for all).

like image 388
Szabolcs Avatar asked Jul 28 '11 00:07

Szabolcs


4 Answers

Drepper's What Every Programmer Should Know About Memory [pdf] is a good reference to one aspect of low-level optimisation.

like image 145
caf Avatar answered Oct 17 '22 20:10

caf


For Intel architectures this is priceless: The Software Optimization Cookbook, Second Edition

like image 23
Remus Rusanu Avatar answered Oct 17 '22 19:10

Remus Rusanu


It's been a few years since I read it, but Write Great Code, Volume 2: Thinking Low-Level, Writing High-Level by Randall Hyde was quite good. It gives good examples of how C/C++ code translates into assembly, e.g. what really happens when you have a big switch statement.

Also, altdevblogaday.com is focused on game development, but the programming articles might give you some ideas.

like image 6
celion Avatar answered Oct 17 '22 20:10

celion


An interesting book about bit manipulation and smart ways of doing low-level things is Hacker's Delight.

This is definitely worth a read for everyone interested in low-level coding.

like image 6
murrekatt Avatar answered Oct 17 '22 19:10

murrekatt