Performance difference between pattern matching and if-else

前端 未结 2 2005
温柔的废话
温柔的废话 2021-02-15 17:06

Why can OCaml generate efficient machine code for pattern matching and not for if-else tests?

I was reading Real World OCaml and I came across this section where they co

2条回答
  •  一个人的身影
    2021-02-15 17:23

    Of course, it is possible to optimise the if-else test the same way. For example, you could write a new optimiser that works in 2 stages: first transform all if-else tests to pattern matching where possible (still in OCaml) and then run the existing compiler.

    Therefore, the answer must be that optimising if-else tests the same way isn't high on the priority list of the compiler developers.

    As a future release of the compiler may bring an even better optimisation for the if-else test, I would only change time-critical code now.

提交回复
热议问题