Last active
May 25, 2024 10:19
-
-
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/6c45a6f4010c29aa62c05afb71c2d039980877f7
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 NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// 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