8. NIFs
8 NIFs
This section outlines an example of how to solve the example problem in Problem Example
by using Native Implemented Functions (NIFs).
NIFs were introduced in Erlang/OTP R13B03 as an experimental feature. It is a simpler and more efficient way of calling C-code than using port drivers. NIFs are most suitable for synchronous functions, such as foo
and bar
in the example, that do some relatively short calculations without side effects and return the result.
A NIF is a function that is implemented in C instead of Erlang. NIFs appear as any other functions to the callers. They belong to a module and are called like any other Erlang functions. The NIFs of a module are compiled and linked into a dynamic loadable, shared library (SO in UNIX, DLL in Windows). The NIF library must be loaded in runtime by the Erlang code of the