Skip to content

Commit 9dd0ad0

Browse files
author
kotlin-samples-pusher-bot
committed
test(samples): add new samples
1 parent 9d6fc54 commit 9dd0ad0

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import kotlin.math.*
2+
3+
fun main() {
4+
val num = 27
5+
val negNum = -num
6+
7+
println("The cube root of ${num.toDouble()} is: " +
8+
cbrt(num.toDouble()))
9+
println("The cube root of ${negNum.toDouble()} is: " +
10+
cbrt(negNum.toDouble()))
11+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import kotlin.time.*
2+
fun main() {
3+
//sampleStart
4+
val timeSource = TimeSource.Monotonic
5+
val mark1 = timeSource.markNow()
6+
Thread.sleep(500) // Sleep 0.5 seconds
7+
val mark2 = timeSource.markNow()
8+
9+
// Before 1.8.0
10+
repeat(4) { n ->
11+
val elapsed1 = mark1.elapsedNow()
12+
val elapsed2 = mark2.elapsedNow()
13+
14+
// Difference between elapsed1 and elapsed2 can vary depending
15+
// on how much time passes between the two elapsedNow() calls
16+
println("Measurement 1.${n + 1}: elapsed1=$elapsed1, " +
17+
"elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
18+
}
19+
println()
20+
21+
// Since 1.8.0
22+
repeat(4) { n ->
23+
val mark3 = timeSource.markNow()
24+
val elapsed1 = mark3 - mark1
25+
val elapsed2 = mark3 - mark2
26+
27+
// Now the elapsed times are calculated relative to mark3,
28+
// which is a fixed value
29+
println("Measurement 2.${n + 1}: elapsed1=$elapsed1, " +
30+
"elapsed2=$elapsed2, diff=${elapsed1 - elapsed2}")
31+
}
32+
// It's also possible to compare time marks with each other
33+
// This is true, as mark2 was captured later than mark1
34+
println(mark2 > mark1)
35+
//sampleEnd
36+
}

0 commit comments

Comments
 (0)