Last active
February 3, 2026 20:21
-
-
Save dacr/4016404c24d38fa667b072686f55575c to your computer and use it in GitHub Desktop.
Simple operations duration tools. / published by https://github.com/dacr/code-examples-manager #08446962-f8ca-4996-b6b9-4ede82ade95f/8eb2543a160912ab865b60c3d2d51386622b7b68
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // summary : Simple operations duration tools. | |
| // keywords : scala, duration, howlong, @testable | |
| // publish : gist | |
| // authors : David Crosson | |
| // license : Apache License Version 2.0 (https://www.apache.org/licenses/LICENSE-2.0.txt) | |
| // id : 08446962-f8ca-4996-b6b9-4ede82ade95f | |
| // created-on : 2020-07-07T06:55:19Z | |
| // managed-by : https://github.com/dacr/code-examples-manager | |
| // run-with : scala-cli $file | |
| // --------------------- | |
| //> using scala "3.4.2" | |
| // --------------------- | |
| import scala.util.{Try, Success, Failure} | |
| def now(): Long = System.currentTimeMillis() | |
| def howLong[T](processing: => T): (Try[T], Long) = { | |
| val started = now() | |
| (Try(processing), now() - started) | |
| } | |
| def howLongReport[T](text: String)(processing: => T): Unit = { | |
| val (response, duration) = howLong(processing) | |
| response match { | |
| case Success(result) => println(s"Success after ${duration}ms : $result $text") | |
| case Failure(exception) => println(s"Failure after ${duration}ms : $text") | |
| } | |
| } | |
| howLongReport("as the sum value") { | |
| 1.to(100000).sum | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment