diff --git a/README.md b/README.md index 563cfcb7..7b83d2bf 100644 --- a/README.md +++ b/README.md @@ -2338,6 +2338,24 @@ public class FormatBytesSnippet { ## Thread +## Execution Time Measurement +```java +public class ExecutionTimeMeasurementSnippet { + + /** + * Measures the time taken to execute a task. + * + * @param task to execute + * @return time taken in milliseconds + */ + public static long measure(Runnable task) { + long start = System.currentTimeMillis(); + task.run(); + return System.currentTimeMillis() - start; + } +} +``` + ### Thread Pool ### Thread diff --git a/src/main/java/thread/ExecutionTimeMeasurementSnippet.java b/src/main/java/thread/ExecutionTimeMeasurementSnippet.java new file mode 100644 index 00000000..d5385444 --- /dev/null +++ b/src/main/java/thread/ExecutionTimeMeasurementSnippet.java @@ -0,0 +1,43 @@ +/* + * MIT License + * + * Copyright (c) 2017-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package thread; + +/** + * ExecutionTimeMeasurementSnippet. + */ +public class ExecutionTimeMeasurementSnippet { + + /** + * Measures the time taken to execute a task. + * + * @param task to execute + * @return time taken in milliseconds + */ + public static long measure(Runnable task) { + long start = System.currentTimeMillis(); + task.run(); + return System.currentTimeMillis() - start; + } +} diff --git a/src/test/java/thread/ExecutionTimeMeasurementSnippetTest.java b/src/test/java/thread/ExecutionTimeMeasurementSnippetTest.java new file mode 100644 index 00000000..bb3e8c07 --- /dev/null +++ b/src/test/java/thread/ExecutionTimeMeasurementSnippetTest.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2017-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package thread; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.junit.jupiter.api.Test; + +class ExecutionTimeMeasurementSnippetTest { + + @Test + void measure_shouldReturnApproximateSleepTime() { + long duration = ExecutionTimeMeasurementSnippet.measure(() -> { + try { + Thread.sleep(100); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + }); + + assertTrue(duration >= 95 && duration <= 200, "Execution time should be close to 100 ms"); + } +} \ No newline at end of file