Question

I am using non-local jumps (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 ?

Thanks.

Was it helpful?

Solution

Like Jens said, if it ever becomes a noticeable bottleneck, redesign it since that is not how setjmp is supposed to be used.

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. setjmp and longjmp on the other hand need to save and restore all registers in order to resume execution after the jump. 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.

I have no idea if less work is required on x86, but I would suspect that goto is a lot cheaper there too.

OTHER TIPS

setjmp has to save sufficient information for the program to continue execution when longjmp is called. This will typically consist of the current stack pointer, along with the current values of any other CPU registers that could affect the computation.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top