Advertisement
paranid5

Kotlin vs Java

Jan 31st, 2021
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 4.26 KB | None | 0 0
  1. import java.time.LocalTime
  2.  
  3. fun main() {
  4.     // --------------------------------------- JAVA ARRAY LIST -------------------------------------------------------
  5.     val javaArrayList = java.util.ArrayList<Long>()
  6.     val start1 = LocalTime.now()!!
  7.  
  8.     for (i in 0..1e5.toLong())
  9.         javaArrayList.add(i)
  10.  
  11.     println("JAVA LIST ADD:   ${LocalTime.now()!!.toNanoOfDay() - start1.toNanoOfDay()}")
  12.  
  13.     val start11 = LocalTime.now()!!
  14.  
  15.     while (javaArrayList.isNotEmpty())
  16.         javaArrayList.remove(javaArrayList[javaArrayList.size / 2])
  17.  
  18.     println("JAVA LIST REMOVE:     \t\t\t${LocalTime.now()!!.toNanoOfDay() - start11.toNanoOfDay()}")
  19.  
  20.     // --------------------------------------- JAVA VECTOR -------------------------------------------------------
  21.  
  22.     val javaVector = java.util.Vector<Long>()
  23.     val start2 = LocalTime.now()!!
  24.  
  25.     for (i in 0..1e5.toLong())
  26.         javaVector.add(i)
  27.  
  28.     println("JAVA VECTOR ADD: ${LocalTime.now()!!.toNanoOfDay() - start2.toNanoOfDay()}")
  29.  
  30.     val start22 = LocalTime.now()!!
  31.  
  32.     while (javaVector.isNotEmpty())
  33.         javaVector.remove(javaVector[javaVector.size / 2])
  34.  
  35.     println("JAVA VECTOR REMOVE:   \t\t\t${LocalTime.now()!!.toNanoOfDay() - start22.toNanoOfDay()}")
  36.  
  37.     // --------------------------------------- KOTLIN MUTABLE LIST  -------------------------------------------------------
  38.  
  39.     val kotlinMutableList = MutableList<Long>(0) { 0 }
  40.     val start3 = LocalTime.now()!!
  41.  
  42.     for (i in 0..1e5.toLong())
  43.         kotlinMutableList.add(i)
  44.  
  45.     println("KOTLIN LIST ADD: ${LocalTime.now()!!.toNanoOfDay() - start3.toNanoOfDay()}")
  46.  
  47.     val start33 = LocalTime.now()!!
  48.  
  49.     while (kotlinMutableList.isNotEmpty())
  50.         kotlinMutableList.remove(kotlinMutableList[kotlinMutableList.size / 2])
  51.  
  52.     println("KOTLIN LIST REMOVE:   \t\t\t${LocalTime.now()!!.toNanoOfDay() - start33.toNanoOfDay()}")
  53.  
  54.     // --------------------------------------- JAVA HASH SET -------------------------------------------------------
  55.  
  56.     val javaSet = java.util.HashSet<Long>()
  57.     val start4 = LocalTime.now()!!
  58.  
  59.     for (i in 0..1e5.toLong())
  60.         javaSet.add(i)
  61.  
  62.     for (i in 0..1e5.toLong())
  63.         javaSet.add(i)
  64.  
  65.     println("JAVA SET ADD:    ${LocalTime.now()!!.toNanoOfDay() - start4.toNanoOfDay()}")
  66.  
  67.     val start44 = LocalTime.now()!!
  68.  
  69.     while (javaSet.isNotEmpty())
  70.         javaSet.remove(javaSet.first())
  71.  
  72.     println("JAVA SET REMOVE:      \t\t\t${LocalTime.now()!!.toNanoOfDay() - start44.toNanoOfDay()}")
  73.  
  74.     // --------------------------------------- KOTLIN SET -------------------------------------------------------
  75.  
  76.     val kotlinSet = mutableSetOf<Long>()
  77.     val start5 = LocalTime.now()!!
  78.  
  79.     for (i in 0..1e5.toLong())
  80.         kotlinSet.add(i)
  81.  
  82.     for (i in 0..1e5.toLong())
  83.         kotlinSet.add(i)
  84.  
  85.     println("KOTLIN SET ADD:  ${LocalTime.now()!!.toNanoOfDay() - start5.toNanoOfDay()}")
  86.  
  87.     val start55 = LocalTime.now()!!
  88.  
  89.     while (kotlinSet.isNotEmpty())
  90.         kotlinSet.remove(kotlinSet.first())
  91.  
  92.     println("KOTLIN SET REMOVE:    \t\t\t${LocalTime.now()!!.toNanoOfDay() - start55.toNanoOfDay()}")
  93.  
  94.     // --------------------------------------- JAVA DEQUE -------------------------------------------------------
  95.  
  96.     val javaDeque = java.util.ArrayDeque<Long>()
  97.     val start6 = LocalTime.now()!!
  98.  
  99.     for (i in 0..1e5.toLong())
  100.         javaDeque.add(i)
  101.  
  102.     println("JAVA DEQUE ADD:  ${LocalTime.now()!!.toNanoOfDay() - start6.toNanoOfDay()}")
  103.  
  104.     val start66 = LocalTime.now()!!
  105.  
  106.     while (javaDeque.isNotEmpty())
  107.         javaDeque.remove(javaDeque.first)
  108.  
  109.     println("JAVA DEQUE REMOVE:    \t\t\t${LocalTime.now()!!.toNanoOfDay() - start66.toNanoOfDay()}")
  110.  
  111.     // --------------------------------------- KOTLIN DEQUE -------------------------------------------------------
  112.  
  113.     val kotlinDeque = ArrayDeque<Long>()
  114.     val start7 = LocalTime.now()!!
  115.  
  116.     for (i in 0..1e5.toLong())
  117.         kotlinDeque.add(i)
  118.  
  119.     println("KOTLIN DEQUE ADD:${LocalTime.now()!!.toNanoOfDay() - start7.toNanoOfDay()}")
  120.  
  121.     val start77 = LocalTime.now()!!
  122.  
  123.     while (kotlinDeque.isNotEmpty())
  124.         kotlinDeque.remove(kotlinDeque.first())
  125.  
  126.     println("KOTLIN DEQUE REMOVE: \t\t\t${LocalTime.now()!!.toNanoOfDay() - start77.toNanoOfDay()}")
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement