Sunday 15 February 2015

performance - Erlang : Which is more efficient in this scenario - using try catch or case statements? -


Say I have some functions in Arlon (1) which {ok, result} The function was successfully executed and {error, "error region"} if there was an error

Now in another function fn2 () I call fn1 () And I need to check the result of fn1 and if it is {ok, result} , then go ahead.

I thought, I can do this or try to catch it from any case but ability is my main concern and I would like to know that the two methods given below Which of the following is more efficient:

try-catch method

  fn2 () - & gt; Try {OK, result} = fn1 (),% Hold something with the result Catch OK: Duration - & gt; Period; Exit: reason - & gt; {Exit, reason}; Error: cause - & gt; {Error, {cause, erlang: get_stacktrace ()}} end  

case method

  Fn2 () - & gt; Res = fn1 (), Res of the case {ok, Result} - & gt; Do something right with the result; {Error, reason} - & gt; The end of the reason  

The case method will be more efficient, because it is only pattern match, And not include making call stacks and accessories.

In both instances, you are going to handle "error" locally, so there is no point in trying to catch up. What you can sometimes see is something like this:

  fn2 () -> {OK, RESULT} = FN1 (),% What is the good stuff with the result  

Here is the intention that if you throw fn2 () a badmatch, if fn1 () fix Do not let anyone handle you "above" problem. Like it can kill your process, and make your supervisor a new one.


No comments:

Post a Comment