Measure execution time forth

Viewed 200

How to measure execution time of my program? I`ve found this but it doesnot work for me because my program needs some numbers in stack to work so my program and this solution kind of interfiering with each other(as I understand because when i try time: myfunc or time: num1 num2 myfunc nothing works....)

: time: ( "word" -- )
  utime 2>R ' EXECUTE
  utime 2R> D-
  <# # # # # # # [CHAR] . HOLD #S #> TYPE ."  seconds" ;

thanks for any help

1 Answers

The tick ' will parse the text immediately following the call to time:. So for a call with arguments it should be num1 num2 time: myfunc

Also, the tick will parse the input stream in run time. If you have the call to time: embedded in the word definition, it will try to get the execution toke for the word from the input stream, not for the word immediately following the time:. Use ['] to get an xt of the next word in compile time, and pass that xt as argument to time:

: time ( xt -- )
  utime 2>R EXECUTE
  utime 2R> D-
  <# # # # # # # [CHAR] . HOLD #S #> TYPE ."  seconds" ;

: foo 0 1000000 0 do i + loop drop ;

: bar num1 num2 ['] foo time ; \ pass the foo's xt as argument to time

Note there's no ' call in time, the EXECUTE will grab the token from the stack.

Related