开发者

(meaningful) cost of the jump instruction?

开发者 https://www.devze.com 2023-02-13 09:35 出处:网络
I\'m sorry... (premature) optimization is the root of all evil, but I\'d like to know: how much more costly is to have a jmp instruction vs. not have it (branchless code)? I\'m aft开发者_C百科er the m

I'm sorry... (premature) optimization is the root of all evil, but I'd like to know: how much more costly is to have a jmp instruction vs. not have it (branchless code)? I'm aft开发者_C百科er the methodology for knowing how to calculate these things. This is purely exploratory, not practical, I'm trying to find my way into a theoretical problem and my testing code brought this up. Thanks.


You'll have to test it on your architecture if you really want to know.

But in general, on modern processors, there is minimal cost for an unconditional jump. It's basically pretty much free apart from a very small amount of instruction cache overhead. It will probably get executed in parallel with neighbouring instructions so might not even cost you a clock cycle. This is because the jump can be executed by one of several parallel execution units.

Look at it this way - a single read of main memory is probably 100-200 times more expensive.

It's a subset of branch prediction more generally, but there no risk of a branch misprediction so you are safe from having to flush the instruction pipeline, which is the main cost associated with conditional jumps.


Well, it is better not to jump. In the early days conditional jumps were written so that the most probable condition wouldn't cause a jump. So not needing any instruction at all is obviously faster, but I can't tell you how costly it is compared to other instructions. Maybe you should measure a billion jumps one way or the other...

0

精彩评论

暂无评论...
验证码 换一张
取 消