import java.util.*;
import java.util.function.*;
import java.lang.*;
import java.io.*;
import java.util.concurrent.TimeUnit;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        TimeLoggerUtils.logExecutionTime("Task 1", () -> {
            sleep(100);
            System.out.println("Hello world!");
        });

        int sum = TimeLoggerUtils.logExecutionTime("Task 2", () -> {
            sleep(200);
            return 1 + 1;
        });

        System.out.println("Sum: " + sum);
    }

    public static void sleep(long millis) {
        try {
            Thread.sleep(millis);
        } catch (Exception ignored) {
            
        }
    }
}

final class TimeLoggerUtils {

    /**
     * Logs the execution time of a task that returns a value, using a specific standard TimeUnit.
     *
     * @param taskName Name of the task for logging.
     * @param unit     The standard time unit (java.util.concurrent.TimeUnit) for logging the duration.
     * @param task     The task to execute (Supplier).
     * @param <T>      The return type of the task.
     * @return The result of the task execution.
     */
    public static <T> T logExecutionTime(String taskName, TimeUnit unit, Supplier<T> task) {
        long startTime = System.nanoTime(); // Usar nanoTime para medir intervalos
        try {
            return task.get();
        } finally {
            calculateAndLogExecutionTime(taskName, startTime, unit);
        }
    }

    /**
     * Logs the execution time of a task that does not return a value, using a specific standard TimeUnit.
     *
     * @param taskName Name of the task for logging.
     * @param unit     The standard time unit (java.util.concurrent.TimeUnit) for logging the duration.
     * @param task     The task to execute (Runnable).
     */
    public static void logExecutionTime(String taskName, TimeUnit unit, Runnable task) {
        long startTime = System.nanoTime(); // Usar nanoTime para medir intervalos
        try {
            task.run();
        } finally {
            calculateAndLogExecutionTime(taskName, startTime, unit);
        }
    }

    /**
     * Logs the execution time of a task that returns a value, defaulting to MILLISECONDS.
     *
     * @param taskName Name of the task for logging.
     * @param task     The task to execute (Supplier).
     * @param <T>      The return type of the task.
     * @return The result of the task execution.
     */
    public static <T> T logExecutionTime(String taskName, Supplier<T> task) {
        // Llama a la versión sobrecargada con TimeUnit.MILLISECONDS por defecto
        return logExecutionTime(taskName, TimeUnit.MILLISECONDS, task);
    }

    /**
     * Logs the execution time of a task that does not return a value, defaulting to MILLISECONDS.
     *
     * @param taskName Name of the task for logging.
     * @param task     The task to execute (Runnable).
     */
    public static void logExecutionTime(String taskName, Runnable task) {
        // Llama a la versión sobrecargada con TimeUnit.MILLISECONDS por defecto
        logExecutionTime(taskName, TimeUnit.MILLISECONDS, task);
    }


    /**
     * Calculates the elapsed time since startTime and logs it using the specified standard TimeUnit.
     *
     * @param taskName  Name of the task.
     * @param startTime Start time in nanoseconds (from System.nanoTime()).
     * @param unit      The desired standard time unit (java.util.concurrent.TimeUnit) for logging.
     */
    private static void calculateAndLogExecutionTime(String taskName, long startTime, TimeUnit unit) {
        long endTime = System.nanoTime();
        long durationNanos = endTime - startTime;
        
        double durationInUnit = (double) durationNanos / unit.toNanos(1);

        String logMessage = String.format("%s execution time: %.3f %s", taskName, durationInUnit, unit.name().toLowerCase());
        System.out.println(logMessage);
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: