Class Timing

java.lang.Object
org.kiwiproject.beta.time.Timing

@Beta public final class Timing extends Object
Timing utilities that provide a convenient way to measure elapsed time of operations.
  • Method Details

    • timeWithResult

      public static <R> Timing.TimedWithResult<R> timeWithResult(org.apache.commons.lang3.time.StopWatch stopWatch, Supplier<R> operation)
      Time an operation that returns a result using StopWatch. Any exception thrown by the Supplier is propagated (not caught).

      The StopWatch is reset before starting. It is always stopped, even if the operation throws an exception.

      If you need the elapsed time when an exception is thrown, you can simply call StopWatch.getTime().

      Since StopWatch is not thread-safe, callers are responsible for ensuring thread-safety. Passing it as an argument permits timing more than one sequential operation in the same method, but could lead to problems if callers are not careful. Typical use is to instantiate the StopWatch within a method and use it only within that method.

      Type Parameters:
      R - the type of result returned by the operation
      Parameters:
      stopWatch - the StopWatch to use
      operation - the operation to time
      Returns:
      a Timing.TimedWithResult containing the elapsed time and the result of the operation
    • timeNoResult

      public static Timing.TimedNoResult timeNoResult(org.apache.commons.lang3.time.StopWatch stopWatch, Runnable operation)
      Time an operation that does not return a result using StopWatch. Any exception thrown by the Supplier is propagated (not caught).

      The StopWatch is reset before starting. It is always stopped, even if the operation throws an exception.

      If you need the elapsed time when an exception is thrown, you can simply call StopWatch.getTime().

      Since StopWatch is not thread-safe, callers are responsible for ensuring thread-safety. Passing it as an argument permits timing more than one sequential operation in the same method, but could lead to problems if callers are not careful. Typical use is to instantiate the StopWatch within a method and use it only within that method.

      Parameters:
      stopWatch - the StopWatch to use
      operation - the operation to time
      Returns:
      a Timing.TimedNoResult containing the elapsed time of the operation