floatTiming.frink

View or download floatTiming.frink in plain text format


/** This program tests floating-point timing for a large number of digits.  It
also calculates the Big-O notation (which should be O(n^something)) where n is
the number of digits being calculated. */


runs = million

lastTime = undef
lastIterations = undef
lastPrecision = undef

n1 = 1.
d1 = 7.

n2 = 2
d2 = 17.

for n = 1 to 3
{
   lastIterations = 0
   lastTime = 0 s
   lastPrec = 0
   
   multifor [p, m] = [1 to 7, [1,2]]
   {
      prec = m 10^p
      iterations = ceil[10^(7-p) / m]
      
      setPrecision[prec]
      a = n1/d1
      b = n2/d2

      start = now[]
      for r = 1 to iterations
      {
         c = a / b
      }
      end = now[]

      setPrecision[20]
      time = end-start

      print["precision=$prec  "]
      if m == 2
         print[" "]
      print["iterations=$iterations  "]
      print["Time:" (time -> "ms")]

      // Calculate Big-O exponent O(n^x)
      if lastTime != undef and lastTime != 0 s and time != 0 s and lastIterations != 0 and iterations != 0 and lastPrec != 0
      {
         iterTime = time/iterations
         lastIterTime = lastTime / lastIterations
         x = log[iterTime/lastIterTime] / log[prec/lastPrec]
         // t = c prec^x     (solve for c)
         // Constant factor c solved via
         // https://frinklang.org/fsp/solve.fsp?eq=t+%3D+c+prec%5Ex&solveFor=c
         c = prec^(-x) iterTime
         println["  O(n^" + formatFix[x,1,3] + ")\t" + formatSig[c,"s",6] + " n^" + formatFix[x,1,3]]
      } else
      println[]

      lastTime = time
      lastPrec = prec
      lastIterations = iterations
   }
}


View or download floatTiming.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 17954 days, 2 hours, 13 minutes ago.