How a program runs
What happens under the code you write, from a pointer to the cache to the scheduler that decides nothing is happening right now.
- Why does 2 + 3 * 4 equal 14 and not 20?The multiplication is done first: three times four makes twelve, and only then is the two added, giving fourteen. Reading left to right would give twenty.
- How many samples does a profiler need before a percentage in the report means anything?It depends on how many samples landed on that line: you need a few hundred samples on the line you care about, because the relative error is roughly one over the square root of that count.
- If I make one part of my program twice as fast, how much faster does the whole program get?Less than twice as fast: only the share of run time you actually touch can improve, so the untouched remainder caps the gain, often at a few per cent.
- Why does reading one byte from memory load 64 bytes?Memory hardware moves data in fixed sixty-four-byte blocks, so a one-byte read fetches the whole block; the extra bytes cost almost nothing because reaching the memory row costs most of the time.
- Why can two threads that each work correctly still give the wrong answer?Each thread reads the shared value and writes it back in two separate steps, so the other thread can run in between and one increment silently overwrites the other.
- My change made it 4% faster — how do I know that isn't just noise?You cannot tell from one pair of runs. Run the unchanged program ten times first: only then does a four per cent gap mean anything.
- Why does the compiler catch a type mismatch but not division by zero?A compiler checks types before running because types are known for every possible run; it cannot know a divisor is zero until the program actually runs.
- Why does every program I run see the same memory addresses?A pointer's number is a private label for a block of memory, not a physical chip location, so two programs can use the same label for different places.
- Why is a system call so much slower than calling a function?A system call is slow because the processor has to switch into the kernel and back, and that fixed cost is paid in full even when the request itself is trivial.
- Why does my program have to ask the operating system just to read a file?The processor refuses to run the instructions that touch a disk or change memory mappings in your program's limited mode, so your code must trap into the kernel and ask it to do the read.
- How does the CPU avoid walking the page table on every single memory access?A small cache of recent translations, called the TLB, is checked on every memory access; a hit skips the page-table walk, and a miss walks once then stores the result.
- Why does sorting an array make a loop that scans it and counts matches so much faster?Sorting does not make the comparisons cheaper. It makes the test inside the loop predictable, so the processor stops guessing wrong and stops paying for those mistakes.
The other subjects
If you want the whole thing
Behind every answer here is a course that works through its subject from the beginning, with problems under each idea and a reason rather than a tick when one is marked. An answer settles the question you came with; the course is what makes the next one easier.
The libraryAsk for a course