[英]Are there performance problems with non-local jumps?
I am using non-local jumps ( setjmp
, longjmp
). 我正在使用非本地跳转(
setjmp
, longjmp
)。 I would like to know if it can be a problem for the performances. 我想知道这是否会对演出造成影响。 Does
setjmp
save all the stack, or just some pointers ? setjmp
保存所有堆栈还是仅保存一些指针?
Thanks. 谢谢。
setjmp
has to save sufficient information for the program to continue execution when longjmp
is called. setjmp
必须保存足够的信息,以便在调用longjmp
时程序可以继续执行。 This will typically consist of the current stack pointer, along with the current values of any other CPU registers that could affect the computation. 它通常由当前堆栈指针以及可能影响计算的任何其他CPU寄存器的当前值组成。
I can't comment on whether this causes a "performance problem", because I don't know what you want to compare it against. 我无法评论这是否会导致“性能问题”,因为我不知道您要与之进行比较。
The quick answer is: not very likely. 快速的答案是:不太可能。 If
setjmp
ever becomes a noticeable bottleneck in your program, I'd tend to say your program design needs an overhaul. 如果
setjmp
曾经成为您程序中的明显瓶颈,我倾向于说您的程序设计需要大修。
Like Jens said, if it ever becomes a noticeable bottleneck, redesign it since that is not how setjmp
is supposed to be used. 就像Jens所说的那样,如果它成为一个明显的瓶颈,请重新设计它,因为这不是应该使用
setjmp
方式。
As for your question: 至于你的问题:
This probably depends on which architecture you are running your program on and exactly what the compiler does with your code. 这可能取决于您在其上运行程序的体系结构以及编译器对代码的确切作用。 On ARM,
goto
is probably translated into a single branch instruction which is quite fast. 在ARM上,
goto
可能会转换为一条非常快的分支指令。 setjmp
and longjmp
on the other hand need to save and restore all registers in order to resume execution after the jump. 另一方面,
setjmp
和longjmp
需要保存和恢复所有寄存器,以便在跳转后恢复执行。 On an ARMv7-a with NEON support, this would require saving roughly 16 32-bit registers and up to 16 128-bit registers which is quite a bit of extra work compared to a simple branch. 在支持NEON的ARMv7-a上,这将需要保存大约16个32位寄存器和多达16个128位寄存器,与简单分支相比,这需要很多工作。
I have no idea if less work is required on x86, but I would suspect that goto
is a lot cheaper there too. 我不知道在x86上是否需要较少的工作,但是我怀疑
goto
在那里也便宜很多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.