MetaTalk benchmark tests

on repeatloop1 count
  repeat for count times
  end repeat
end repeatloop1

on repeatloop2 count
  repeat with i = 1 to count
  end repeat
end repeatloop2

on ifact1loop count
  repeat with i = 1 to count
    put 1.0 into x
    repeat with j = 1 to 100
      multiply j by x
    end repeat
  end repeat
end ifact1loop

on ifact2loop count
  repeat with i = 1 to count
    put 1.0 into x
    repeat with j = 1 to 100
      if j <> 1.0
      then multiply j by x
    end repeat
  end repeat
end ifact2loop

function fact x
  if x <= 1
  then return 1
  else return x * fact(x - 1)
end fact

on factloop count
  repeat with i = 1 to count
    put fact(100) into dummyvar
  end repeat
end factloop

on stems
  put "/usr/dict/words" into fname
  open file fname for read
  read from file fname until eof
  close file fname
  repeat for each word w in it
    if the number of chars in w is 4
    then put w into lastfour
    else if the number of chars in w is 5 and "'" is not in w\
            and (char 5 of w is not "s" or char 1 to 4 of w is not lastfour)
         then put w after wordlist[char 1 to 3 of w]
  end repeat
  put keys(wordlist) into sorted
  sort lines of sorted
  repeat for each word w in sorted
    put wordlist[w] into t
    put the number of chars in t div 5 into nwords
    if nwords > 1 then
      put w && nwords & tab & char 1 to 5 of t & return after outputstring
      repeat with n = 1 to nwords - 1
        put tab & char n * 5 + 1 to n * 5 + 5 of t\
		 & return after outputstring
      end repeat
    end if
  end repeat
  put outputstring
end stems

on stemsloop count
  repeat with i = 1 to count
    stems
  end repeat
end stemsloop

on shellloop count
  repeat for count times
    put shell("echo test >/dev/null") into dummyvar
  end repeat
end shellloop

on fileloop count
  put "/tmp/tmp" into MYOUTPUT
  repeat for count times
    open file MYOUTPUT for write
    repeat with j = 1 to 100 times
	write "LINE ->" && j & return to file MYOUTPUT 
    end repeat
    close file MYOUTPUT
    put 0 into j
    open file MYOUTPUT for read
    repeat until the result is "eof"
	put j + 1 into j
        read from file MYOUTPUT for 1 line
    end repeat
    close file MYOUTPUT
    if j < 100
    then put "WARNING: Retrieved only" && j && "lines!"
  end repeat
end fileloop

on startup
  put 1000000 into NREPEAT
  put 10000 into NIFACT1
  put 10000 into NIFACT2
  put 10000 into NFACT
  put 1000 into NSYSTEM
  put 2000 into NFILE
  put 10 into NSTEM

  put 0 into overall

  put the long seconds into beforev
  repeatloop1 NREPEAT
  put the long seconds - beforev into total
  put NREPEAT && "repeat for x times took" && total && "seconds"
  add total to overall

  put the long seconds into beforev
  repeatloop2 NREPEAT
  put the long seconds - beforev into total
  put NREPEAT && "repeat with i took" && total && "seconds"
  add total to overall

  put the long seconds into beforev
  ifact1loop NIFACT1
  put the long seconds - beforev into total
  put NIFACT1 && "iterative factorial(100) took" && total && "seconds"
  add total to overall

  put the long seconds into beforev
  ifact2loop NIFACT2
  put the long seconds - beforev into total
  put NIFACT2 && "iterative factorial(100) with 'if' took"&& total && "seconds"
  add total to overall

  put the long seconds into beforev
  factloop NFACT
  put the long seconds - beforev into total
  put NFACT && "recursive factorial(100) took" && total && "seconds"
  add total to overall

  put the long seconds into beforev
  shellloop NSYSTEM
  put the long seconds - beforev into total
  put NSYSTEM && "shell() calls took" && total && "seconds"
  add total to overall

  put the long seconds into beforev
  fileloop NFILE
  put the long seconds - beforev into total
  put NFILE && "100 line write and read took" && total && "seconds"
  add total to overall

  put "total time" && overall && "seconds"
end startup