Java TimeUnit Usage logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)

Here you can find the source of logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos)

Description

Logs the amount of time that a particular activity took, based on the given start time

License

Open Source License

Parameter

Parameter Description
log the log to write to
prefix text to write to log before the amount of time that the activity took
startTimeNanos the starting time of this iteration, in nanoseconds (e.g., System.nanoTime())

Declaration

public static void logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos) 

Method Source Code


//package com.java2s;
/*//w  ww. j a v a2s  . co m
 * Copyright (C) 2017 University of South Florida.
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.concurrent.TimeUnit;

public class Main {
    /**
     * Logs the amount of time that a particular activity took, based on the given start time
     *
     * @param log            the log to write to
     * @param prefix         text to write to log before the amount of time that the activity took
     * @param startTimeNanos the starting time of this iteration, in nanoseconds (e.g., System.nanoTime())
     */
    public static void logDuration(org.slf4j.Logger log, String prefix, long startTimeNanos) {
        long durationNanos = System.nanoTime() - startTimeNanos;
        long durationMillis = TimeUnit.NANOSECONDS.toMillis(durationNanos);
        long durationSeconds = TimeUnit.NANOSECONDS.toSeconds(durationNanos);
        double elapsedTime = (double) durationSeconds + (double) durationMillis / 1000.0;
        elapsedTime = Math.round(elapsedTime * 1000.0) / 1000.0;
        log.info(prefix + elapsedTime + " seconds");
    }
}

Related

  1. isCurrentDateBeforeAndWithinRangeOfDate( Date date, int rangeInDays)
  2. isTheSameDayCheckSummerTime(final Date day1, final Date day2, final boolean escapeYear)
  3. isTimedOut(long start, long timeout)
  4. isValidTTL(final Integer ttlDurationInSeconds)
  5. julianDayToMillis(int julianDay)
  6. logWatchStop(long start, String... params)
  7. mergeResults(List>> futures)
  8. millisBetweenNanoTimes(long startNanos, long endNanos)
  9. millisecondsToHumanReadable(long duration)