#93 recursive formulas are slow when used with custom function

Christoph Klein

While teting gretl I found out that recursive formulas like are very slow when they use a custom defined formula.
The speed is so slow, that it is even faster to program a loop to do the conversion.
I have attached two test program. slow.inp uses a recursive formula and is, as the name says, quite slow. The second script fast.inp calculates the same result but uses a loop.
Is this a bug or should i try to avoid this stuff in my own programs?


  • slow implementation

  • File Added: fast.inp

  • fast implementation

  • Allin Cottrell
    Allin Cottrell

    Your "slow" script is slow because on each of 15000 iterations gretl
    has to copy the entire input series, given the way you have defined
    the function test1(). Please see the chapter of the Gretl User's Guide
    on "User-defined functions", in particular the subsection titled
    "Variables versus pointers".

    More generally, user-defined functions are expensive, but with this
    example it's difficult to see what you're really trying to do since the
    user-defined function is otiose. The fastest way to do the trivial
    "let y(t) equal y(t-1) plus 1", with an initial value of zero, is

    series y = 0
    y = y(-1) + 1

    For a series length of 15000 this executes on my home machine in 0.01
    seconds, versus 0.68 seconds for your "fast.inp".

  • Allin Cottrell
    Allin Cottrell

    • assigned_to: nobody --> allin
    • status: open --> closed-works-for-me
  • yes, this is just a sample script to show my problem. I'm currently writing my diploma thesis and i want to implement a framework for GARCH processes. The framework should be as generic as can be and different volatility dynamics (EGARCH,TGARCH,..) should be included by a user-defined function within an autoregressive formula.
    I tried to follow your suggestion, but I was not able to use pointers within this formula. I have attached a new sample programm "slowPointer.inp", which fails with

    ? y=test1(&y(-1))+y
    Wrong type of operand for unary '&'
    bad type 66
    eval_ufunc: error evaluating arg 0
    *** autoreg error at obs t = 0 (t1 = 0):
    ret = NULL, p->err = 1
    input = 'y=test1(&y(-1))+y'

    on my computer.
    File Added: slowPointer.inp

  • i have found a solution now. I will use the @macro functionality in order to have flexible and fast autoregressive formulas. This is not as elegant as defining a function, but it works.