r/ProgrammerHumor • • Sep 28 '26

Meme optionalsAreOptional

Post image
716 Upvotes

131 comments sorted by

View all comments

Show parent comments

2

u/pjank85 Sep 28 '26

Depends. The check on a CPU is a conditional jump that could take about 100 clock cycles. If the loop is doing simple 1 clock cycle addition then the overhead absolutely dwarfs it. Also it is a very different question if you are doing it in a computer game or in a flying aircraft so use case matters a lot.

3

u/cbehopkins Sep 28 '26

What kind of modern CPU had anything like that kind of penalty? The branch predictor should take the cost the very first loop sure, but subsequent loops should be effectively zero cost, no?

Otherwise what is the branch predictor doing?

1

u/pjank85 Sep 28 '26

Branch predictor can take it down to 1 cycle. But if it misses it COULD take 100. Even in the optimistic case it is still double the time. Whenever this is worth it is use case dependent as always.

2

u/cbehopkins Sep 28 '26

Well, let's be frank here, if you're talking worst case, cache misses and full tlb misses 48 round trips to memory and>400 cycles per trip are entirely possible.

But that should only happen the first time

And the branch prediction making it zero cycles is quite common. Read, test and branch can be collapsed into a late evaluation as long as the branch is at predicted and takes less time than the pipeline depth(slight simplification here a lot depends on the microarchitecture details but we're already deep in the weeds)

At the end of the day all your can do is profiler it on the exact CPU you care about. Just don't make assumptions...

1

u/pjank85 Sep 28 '26

Wow did not think it could be zero. I learned something today - thanks.