I\'ve often read that exceptions are somewhat slow and should be avoided if performance is an issue (for instance, in Java, F#, etc). Does that apply to common OCaml functio
To first answer the concrete question for Hashtbl.mem before Hashtbl.find:
Don't do it as the check for the existence of the element in the table will have to be dont twice; while the cost will be O(1) for both strategies, calling Hashtbl.mem first will compute the hash-value and the lookup twice -- which can take quite longer than fetching an exception.
As a general advice, only create functions that raise exceptions if the probability of an exception is low. The exceptions are not taken into consideration for the type system, so more robust implementations will have an 'a option return value instead of 'a plus a possible exception. The ocaml core library uses a suffix of _exn to make clear that a function may throw an exception but generally prefers the non-exception throwing ones.
So for the hashtable you should (in a perfect world) have two functions:
Hashtbl.find_exn : 'a t -> key -> 'a (* throws Not_found *)
Hashtbl.find : 'a t -> key -> 'a option
If in doubt, use the second one. If it is not for pure speed, you could write a simple wrapper around the original hash-table:
find_safe h k = try Some (Hashtbl.find h k) with Not_found -> None