Function definition order and performance

Hi, If the matching proceeds from top to bottom, will the same functions defined in different order have different performance strictly? Thanks. Quote from Real World Haskell: "As we have already mentioned, a Haskell implementation checks patterns for matches in the order in which we specify them in our equations. Matching proceeds from top to bottom, and stops at the first success. Equations below a successful match have no effect." Best regards, Zhi-Qiang Lei zhiqiang.lei@gmail.com

Hi,
If the matching proceeds from top to bottom, will the same functions defined in different order have different performance strictly? Thanks.
You should only see a performance difference at all in the case of very complicated (run-time-wise) guards on pattern matches. Usually, your pattern matches will have to be placed in a certain order for *semantic* purposes (i.e. for them to not be overlapping.) I could imagine a scenario in which a complicated guard on a pattern match, which could be otherwise rearranged without changing its semantics, on a function that is itself in the oft-executed loop of an inner function of an algorithm with high complexity *could* have a noticeable impact on performance. In other words, premature optimization is the root of all evil. Are you concerned about a particular case? Example code could help us determine the reason for slowness. Regards, Aleks
participants (2)
-
Aleksandar Dimitrov
-
Zhi-Qiang Lei