Guest User

Untitled

a guest
Jun 25th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. import kotlinx.coroutines.experimental.*
  2. import kotlinx.coroutines.experimental.channels.Channel
  3. import java.util.concurrent.atomic.AtomicLong
  4. import kotlin.concurrent.thread
  5.  
  6. fun main(args: Array<String>) {
  7. // bench("threads") { skynetThread(0, 1000000, 10) } // Out of Memory error
  8. bench("sync") { skynetSync(0, 1000000, 10) }
  9. bench("coroutines-async") { skynetCoroutinesAsync(0, 1000000, 10) }
  10. bench("coroutines-launch") { skynetCoroutinesLaunch(0, 1000000, 10) }
  11. bench("channels") { skynetChannels(0, 1000000, 10) }
  12. }
  13.  
  14. fun bench(name: String, times: Int = 10, func: () -> Long) {
  15. println("Running $name...")
  16. repeat(times) {
  17. val start = System.nanoTime()
  18. val result = func()
  19. val duration = (System.nanoTime() - start) / 1e6
  20. println("result $name: ${result} took: ${duration} ms")
  21. }
  22. }
  23.  
  24. fun skynetSync(num: Int, size: Int, div: Int): Long {
  25. if (size == 1) return num.toLong()
  26. else return (0 until div)
  27. .map { i -> skynetSync(num + i * (size / div), size / div, div) }
  28. .sum()
  29. }
  30.  
  31. fun skynetThread(num: Int, size: Int, div: Int): Long {
  32. if (size == 1) return num.toLong()
  33. val sum = AtomicLong()
  34. repeat(10) { i ->
  35. thread {
  36. val childSum = skynetThread(num + i * (size / div), size / div, div)
  37. sum.addAndGet(childSum)
  38. }
  39. }
  40. return sum.get()
  41. }
  42.  
  43. fun skynetCoroutinesAsync(num: Int, size: Int, div: Int): Long {
  44. fun _child(num: Int, size: Int, div: Int): Deferred<Long> = async {
  45. if (size == 1) num.toLong()
  46. else {
  47. val children = (0 until 10).map { i -> _child(num + i * (size / div), size / div, div) }
  48. children.map { it.await() }.sum()
  49. }
  50. }
  51. return runBlocking { _child(num, size, div).await() }
  52. }
  53.  
  54. fun skynetCoroutinesLaunch(num: Int, size: Int, div: Int): Long {
  55. val sum = AtomicLong()
  56. fun _child(num: Int, size: Int, div: Int): Job = launch {
  57. if (size == 1) sum.addAndGet(num.toLong())
  58. else (0 until 10)
  59. .map { i -> _child(num + i * (size / div), size / div, div) }
  60. .forEach { it.join() }
  61. }
  62. return runBlocking {
  63. _child(num, size, div).join()
  64. sum.get()
  65. }
  66. }
  67.  
  68.  
  69. fun skynetChannels(num: Int, size: Int, div: Int): Long {
  70. val resultChan = Channel<Long>()
  71. fun _child(num: Int, size: Int, div: Int, chan: Channel<Long>): Job = launch {
  72. if (size == 1) chan.send(num.toLong())
  73. else {
  74. var sum = 0L
  75. val sumChan = Channel<Long>()
  76. repeat(10) { i ->
  77. _child(num + i * (size / div), size / div, div, sumChan)
  78. }
  79. repeat(10) {
  80. sum += sumChan.receive()
  81. }
  82. sumChan.close()
  83. chan.send(sum)
  84. }
  85. }
  86. return runBlocking {
  87. _child(num, size, div, resultChan)
  88. resultChan.receive()
  89. }
  90. }
Add Comment
Please, Sign In to add comment