mercredi 7 août 2019

Curious as to why pattern/match on boolean is faster (compute wise) than if/else

Sort of riffing off this question but why is pattern matching on a boolean faster than if/else?

Pattern match is more sophisticated as both a language construct (able to resolve type definitions as well as simple primitives, i.e., booleans) and produces more bytecode. My experience as a software engineer leads me to believe that all things being equal, more code/more complexity = increased run time and decreased performance. Why are we seeing the opposite effect here?

Stylistically it seems that the Scala community has allowed both with no one method being clearly "best practice." But at least from a performance standpoint it does seem that there is a real benefit (albeit small) from preferring pattern matching over if/else. Does this also mean that use of pattern matches should be preferred in all contexts?

Aucun commentaire:

Enregistrer un commentaire