Search code examples
matlabexpressionevalmex

Fastest way of evaluating expression in Matlab


Inputs:

  • arbitrary logical vectors (~2M elements): i1,i2,...,in
  • arbitrary logical expression: eg. "or(and(not(i1),i2),xor(i3,i4))"

Output:

  • resulting logical vector o o=eval("or(and(not(i1),i2),xor(i3,i4))")

Is there any faster way than simulating Matlab's eval function in MEX function? Would the MEX eval be actually faster than native eval? If yes, how many times?


Solution

  • I've implemented eval as a MEX function. I tested it on random expressions of depth 4. My MEX eval is 2x faster than Matlab native eval (15.1s vs 31.4s, 1000 iterations)