Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in AI and Deep Learning by (50.2k points)

There are interpreted languages out there, such as Lisp, Tcl, Perl, etc., that make it easy to define a lambda/proc/sub within your code during runtime and to evaluate it within the same session.

There are compiled languages out there, such as C++, that would execute much faster than the interpreted ones, yet defining a function within a compiled program during runtime and executing it is not easy, if at all possible.

The problem here is to do the following:

  1. Define a function during runtime: for example, based on the initial input data derive an analytic model of the data.

  2. Execute the above function fast in a loop: for example, apply the derived analytic model for analyzing incoming data.

One solution that I saw was not very pretty:

  1. A procedure representing the analytic model was derived in embedded Tcl based on the initial input data.

  2. A lookup table was created by evaluating the procedure in Tcl on an array of sample points that, optimistically speaking, would cover the applicability range.

  3. The lookup table was passed from the Tcl interpreter back to the binary (which was developed in C++).

  4. Then the incoming data was analyzed by interpolating between "close enough" values in the lookup table.

The above solution works but has quite a few problems, both conceptual and computational. Thus the question: is it possible to define a function purely within C++ and make it available for execution within the same runtime session?

Conceptually speaking, is it possible to do something like create a function as a string, compile it in-memory, and somehow link it back into the binary that's being executed?

1 Answer

0 votes
by (108k points)

There are many efficient Lisp compilers out there. There are JIT-compiled VMs like .NET and JVM. There are embeddable C compilers (tcc, clang). It's also pretty easy to generate LLVM IR directly, with all the benefits of a powerful optimizing compiler. 

The C++ Mathematical Expression Toolkit Library is also known as ExprTk has many benefits like:

  • simple to use

  • easy to integrate

  • has an extremely efficient run-time mathematical expression parser and evaluation engine. 

It supports many different forms of functional, logical and vector processing semantics and is very easily extendible.

For further reference, you can refer the following link:

Browse Categories