In Java, a "switch" is more efficient than a serial if blocks, because the compiler generates a tableswitch statement where the target can be determined from the jump table.
In Groovy, the switch is not limited to integer values ββand has a lot of additional semantics, so the compiler cannot use this object. The compiler creates a series of comparisons, exactly the same as for serial blocks.
However, ScriptBytecodeAdapter.isCase(switchValue, caseExpression) is called for each comparison. This is always a dynamic method call for the isCase method of the isCase object. This call is potentially more expensive than ScriptBytecodeAdapter.compareEqual(left, right) , which is called to compare if.
So, in Groovy, a switch is usually more expensive than a serial if blocks.
Ingo kegel
source share