Class Timer

java.lang.Object
ortus.boxlang.runtime.util.Timer

public class Timer extends Object
This class is a utility for timing operations. You can use it as a static helper or as an instance. It contains a map of timers that are started and stopped by label. The label is used to identify the timer.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    The time units it supports
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final DecimalFormat
    The default timing format
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a new timer instance with auto remove timers set to true
    Timer(boolean autoRemoveTimers)
    Create a new timer instance
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clear all timers
    static long
    convert(long time, Timer.TimeUnit timeUnit)
    This method receives a long time in nanoseconds and returns it in the appropriate time unit
    static Timer
    Create a new timer instance with auto remove timers set to true
    static Timer
    create(boolean autoRemoveTimers)
    Create a new timer instance with an auto remove timers option
    Get all timers
    void
    This convenience methods prints out to the console all the timers using the passed milliseconds
    void
    This convenience methods prints out to the console all the timers using the passed timeunit
    start(String label)
    Start a timer with the given label
    stop(String label)
    Stop the timer with the given label and return the elapsed time in the default of milliseconds
    stop(String label, Timer.TimeUnit timeUnit)
    Stop the timer with the given label and return the elapsed time in the given time unit
    long
    stopAndGet(String label, Timer.TimeUnit timeUnit)
    Returns a time in the specified time unit
    long
    Stop the timer with the given label and return the elapsed time in milliseconds
    long
    Stop the timer with the given label and return the elapsed time in nanoseconds
    long
    Stop the timer with the given label and return the elapsed time in seconds
    Stop the timer with the given label, print the elapsed time in the default of milliseconds
    Stop the timer with the given label, print the elapsed time in the default of milliseconds
    static long
    Time the given runnable lambda, print the elapsed time in the default of milliseconds to the console, and return the elapsed time in the given time unit as long
    static long
    timeAndPrint(Runnable runnable, String label)
    Time the given runnable lambda, print the elapsed time in the default of milliseconds to the console, and return the elapsed time in the given time unit as long
    static long
    timeAndPrint(Runnable runnable, String label, Timer.TimeUnit timeUnit)
    Time the given runnable lambda, print the elapsed time in the given time unit to the console, and return the elapsed time in the given time unit as long
    timeIt(Runnable runnable)
    Time the given runnable lambda and return the elapsed time in milliseconds
    timeIt(Runnable runnable, String label)
    Time the given runnable lambda and return the elapsed time in milliseconds
    timeIt(Runnable runnable, Timer.TimeUnit timeUnit)
    Time the given runnable lambda and return the elapsed time in the given time unit
    long
    timeItRaw(Runnable runnable, Timer.TimeUnit timeUnit)
    Time the given runnable lambda and return the elapsed time in the given time unit

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • TIMING_FORMAT

      public static final DecimalFormat TIMING_FORMAT
      The default timing format
  • Constructor Details

    • Timer

      public Timer()
      Create a new timer instance with auto remove timers set to true
    • Timer

      public Timer(boolean autoRemoveTimers)
      Create a new timer instance
      Parameters:
      autoRemoveTimers - Whether or not to auto remove timers when they are stopped
  • Method Details

    • create

      public static Timer create(boolean autoRemoveTimers)
      Create a new timer instance with an auto remove timers option
      Parameters:
      autoRemoveTimers - Whether or not to auto remove timers when they are stopped
      Returns:
      The timer instance
    • create

      public static Timer create()
      Create a new timer instance with auto remove timers set to true
      Returns:
      The timer instance
    • timeAndPrint

      public static long timeAndPrint(Runnable runnable)
      Time the given runnable lambda, print the elapsed time in the default of milliseconds to the console, and return the elapsed time in the given time unit as long
      Parameters:
      runnable - The runnable lambda
    • timeAndPrint

      public static long timeAndPrint(Runnable runnable, String label)
      Time the given runnable lambda, print the elapsed time in the default of milliseconds to the console, and return the elapsed time in the given time unit as long
      Parameters:
      runnable - The runnable lambda
      label - The label to use for the timer output
    • timeAndPrint

      public static long timeAndPrint(Runnable runnable, String label, Timer.TimeUnit timeUnit)
      Time the given runnable lambda, print the elapsed time in the given time unit to the console, and return the elapsed time in the given time unit as long
      Parameters:
      runnable - The runnable lambda
      label - The label to use for the timer output
      timeUnit - The time unit to return. Allowed values are SECONDS, MILLISECONDS, and NANOSECONDS
    • getTimers

      public Map<String,Long> getTimers()
      Get all timers
      Returns:
      The timers map
    • clearTimers

      public void clearTimers()
      Clear all timers
    • printTimers

      public void printTimers(Timer.TimeUnit timeUnit)
      This convenience methods prints out to the console all the timers using the passed timeunit
    • printTimers

      public void printTimers()
      This convenience methods prints out to the console all the timers using the passed milliseconds
    • start

      public Timer start(String label)
      Start a timer with the given label
      Parameters:
      label - The label
      Returns:
      The timer instance
    • timeIt

      public String timeIt(Runnable runnable, Timer.TimeUnit timeUnit)
      Time the given runnable lambda and return the elapsed time in the given time unit
      Parameters:
      runnable - The runnable lambda
      timeUnit - The time unit to return. Allowed values are SECONDS, MILLISECONDS, and NANOSECONDS
      Returns:
      The elapsed time in the given time unit as a string
    • timeItRaw

      public long timeItRaw(Runnable runnable, Timer.TimeUnit timeUnit)
      Time the given runnable lambda and return the elapsed time in the given time unit
      Parameters:
      runnable - The runnable lambda
      timeUnit - The time unit to return. Allowed values are SECONDS, MILLISECONDS, and NANOSECONDS
      Returns:
      The elapsed time in the given time unit as a string
    • timeIt

      public String timeIt(Runnable runnable)
      Time the given runnable lambda and return the elapsed time in milliseconds
      Parameters:
      runnable - The runnable lambda
      Returns:
      The elapsed time in the given time unit as a string
    • timeIt

      public String timeIt(Runnable runnable, String label)
      Time the given runnable lambda and return the elapsed time in milliseconds
      Parameters:
      runnable - The runnable lambda
      label - The label to use for the timer
      Returns:
      The elapsed time in the given time unit as a string
    • stopAndGetSeconds

      public long stopAndGetSeconds(String label)
      Stop the timer with the given label and return the elapsed time in seconds
      Parameters:
      label - The label
      Returns:
      The elapsed time in seconds
    • stopAndGetMillis

      public long stopAndGetMillis(String label)
      Stop the timer with the given label and return the elapsed time in milliseconds
      Parameters:
      label - The label
      Returns:
      The elapsed time in milliseconds
    • stopAndGetNanos

      public long stopAndGetNanos(String label)
      Stop the timer with the given label and return the elapsed time in nanoseconds
      Parameters:
      label - The label
      Returns:
      The elapsed time in nanoseconds
    • stopAndGet

      public long stopAndGet(String label, Timer.TimeUnit timeUnit)
      Returns a time in the specified time unit
      Parameters:
      label -
      timeUnit -
      Returns:
    • stop

      public String stop(String label, Timer.TimeUnit timeUnit)
      Stop the timer with the given label and return the elapsed time in the given time unit
      Parameters:
      label - The label
      timeUnit - The time unit to return. Allowed values are SECONDS, MILLISECONDS, and NANOSECONDS
      Returns:
      The elapsed time in the given time unit as a string: 123.45 seconds
    • stop

      public String stop(String label)
      Stop the timer with the given label and return the elapsed time in the default of milliseconds
      Parameters:
      label - The label
      Returns:
      The elapsed time in milliseconds as a string: 123.45 milliseconds
    • stopAndPrint

      public Timer stopAndPrint(String label)
      Stop the timer with the given label, print the elapsed time in the default of milliseconds
      Parameters:
      label - The label
      Returns:
      The timer instance
    • stopAndPrint

      public Timer stopAndPrint(String label, Timer.TimeUnit timeUnit)
      Stop the timer with the given label, print the elapsed time in the default of milliseconds
      Parameters:
      label - The label
      Returns:
      The timer instance
    • convert

      public static long convert(long time, Timer.TimeUnit timeUnit)
      This method receives a long time in nanoseconds and returns it in the appropriate time unit
      Parameters:
      time - The time in nanoseconds
      timeUnit - The time unit to return. Allowed values are SECONDS, MILLISECONDS, and NANOSECONDS
      Returns:
      The time in the given time unit