Search code examples
stackforth

How can I stop values being outputted immediately in Forth?


Using SwiftForth, I am currently looking at methods for measuring the time it takes for a word to be executed. I am using the words 'counter' and then 'timer' in the form:

counter insert_word_here timer

This immediately outputs the time in microseconds that it takes to run the word. Is there a way I can prevent this integer from being outputted immediately, so that I can store it in the stack?


Solution

  • timer in SwiftForth is implemented something like

    : timer  \ t0 -- ;
      counter swap - u.
    ;
    

    Simply define a word without the u. and the elapsed time in milliseconds is left on the stack.

    : timer-ms \ t0 -- t-elapsed
      counter swap -
    ;
    

    I don't have SwiftForth, but timer was defined as an example on the page I found. I think this should work, but I can't test it.