Skip to content

Instantly share code, notes, and snippets.

@abhijat
Last active March 18, 2019 11:45
Show Gist options
  • Save abhijat/6ac92dcc6344dc2d444d0e0045eecdf5 to your computer and use it in GitHub Desktop.
Save abhijat/6ac92dcc6344dc2d444d0e0045eecdf5 to your computer and use it in GitHub Desktop.
Kotlin ==========================================================
fun flipBit(): Long {
val start = System.currentTimeMillis()
var bit = false
for (i in 1..100000000) {
if (i % 7 == 0) {
bit = !bit
}
}
val end = System.currentTimeMillis()
println("runtime: ${end - start} millis")
return end - start
}
fun main() {
var duration = 0L
for (i in 0..9) {
duration += flipBit()
}
println("avg: ${duration / 10} millis")
}
output ==========================================================
runtime: 917 millis
runtime: 672 millis
runtime: 368 millis
runtime: 311 millis
runtime: 268 millis
runtime: 472 millis
runtime: 474 millis
runtime: 493 millis
runtime: 468 millis
runtime: 513 millis
avg: 495 millis
python ==========================================================
import time
def flip_bit():
start = time.time()
bit = False
for i in range(0, 100000000):
if i % 7 == 0:
bit = not bit
duration = (time.time() - start) * 1000
print('runtime: {} millis'.format(duration))
return duration
if __name__ == '__main__':
duration = 0
for i in range(0, 10):
duration += flip_bit()
print('avg: {} millis'.format(duration / 10.0))
output: ==========================================================
runtime: 7595.5350399 millis
runtime: 6216.00914001 millis
runtime: 6293.36380959 millis
runtime: 6323.05788994 millis
runtime: 6166.88799858 millis
runtime: 6446.27094269 millis
runtime: 6254.33778763 millis
runtime: 6484.94696617 millis
runtime: 5926.96404457 millis
runtime: 5895.44296265 millis
avg: 6360.28165817 millis
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment