Last active
April 2, 2023 10:13
-
-
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/abb810ee5cc44c86406855b411c9b6af45548939
This file contains 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.1.1" | |
// --------------------- | |
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