daily pastebin goal
89%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Kotlin ==========================================================
  2.  
  3. fun flipBit(): Long {
  4.     val start = System.currentTimeMillis()
  5.     var bit = false
  6.     for (i in 1..100000000) {
  7.         if (i % 7 == 0) {
  8.             bit = !bit
  9.         }
  10.     }
  11.  
  12.     val end = System.currentTimeMillis()
  13.     println("runtime: ${end - start} millis")
  14.  
  15.     return end - start
  16. }
  17.  
  18. fun main() {
  19.     var duration = 0L
  20.     for (i in 0..9) {
  21.         duration += flipBit()
  22.     }
  23.     println("avg: ${duration / 10} millis")
  24. }
  25.  
  26. output ==========================================================
  27.  
  28. runtime: 917 millis
  29. runtime: 672 millis
  30. runtime: 368 millis
  31. runtime: 311 millis
  32. runtime: 268 millis
  33. runtime: 472 millis
  34. runtime: 474 millis
  35. runtime: 493 millis
  36. runtime: 468 millis
  37. runtime: 513 millis
  38. avg: 495 millis
  39.  
  40.  
  41. python ==========================================================
  42.  
  43. import time
  44.  
  45.  
  46. def flip_bit():
  47.     start = time.time()
  48.     bit = False
  49.     for i in range(0, 100000000):
  50.         if i % 7 == 0:
  51.             bit = not bit
  52.  
  53.     duration = (time.time() - start) * 1000
  54.  
  55.     print('runtime: {} millis'.format(duration))
  56.     return duration
  57.  
  58.  
  59. if __name__ == '__main__':
  60.  
  61.     duration = 0
  62.     for i in range(0, 10):
  63.         duration += flip_bit()
  64.     print('avg: {} millis'.format(duration / 10.0))
  65.  
  66.  
  67. output: ==========================================================
  68. runtime: 7595.5350399 millis
  69. runtime: 6216.00914001 millis
  70. runtime: 6293.36380959 millis
  71. runtime: 6323.05788994 millis
  72. runtime: 6166.88799858 millis
  73. runtime: 6446.27094269 millis
  74. runtime: 6254.33778763 millis
  75. runtime: 6484.94696617 millis
  76. runtime: 5926.96404457 millis
  77. runtime: 5895.44296265 millis
  78. avg: 6360.28165817 millis
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top