Are you sure that it is actually meaningfully slower? llvm might infer a
switch statement from the common structure, but even if not, you are
talking about a very small number of extra assembly instruction, relative
to say, computing sin(x)
On Thu, Feb 19, 2015 at 4:02 AM Devendra Ghate <[email protected]>
wrote:

> Hello everyone,
>
> Consider following function:
>
> ~~~
> function funca(problem, x)
>    if problem == 1
>       return  x^2;
>    elseif
>        problem == 2
>         return sin(x);
>     end
> end
> ~~~
>
> I have shown only 2 cases. However, there are around 100 cases. These
> `if ... else` statements are being evaluated unnecessarily. If I use
> `switch` structure then it will be faster. But I want to do away with
> these.
>
> Since the arguments and their datatypes remain the same, I don't know
> how to create multiple methods for function.
>
> What is the best strategy to achieve this?
>
> Background:
>
> I am trying to implement a library of ODE solvers for my students.
>
> $y`` + a(x)y` + b(x) = f(x) $
>
> Above function calculates $a(x)$ for a given problem at a specific $x_0$.
>
> So there should various ODE problems defined via the coefficients of
> the derivative terms. My students will then implement various solution
> strategies and compare their performance for these problems. So I need
> to provide
> them with functions `funca` `funcb` `funcf` which give the appropriate
> coefficient depending on the problem.
>
> --
> Cheers,
> Devendra
>

Reply via email to