Created
October 6, 2016 13:22
-
-
Save nmcb/0bbf5e2983e4b6b79f0965f8f74ee7cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def countOneBits(l: Long) = { | |
val Hex55 = 0x5555555555555555L | |
val Hex33 = 0x3333333333333333L | |
val Hex0F = 0x0F0F0F0F0F0F0F0FL | |
val Hex01 = 0x0101010101010101L | |
var x = l - ((l >> 1) & Hex55) | |
x = (x & Hex33) + ((x >> 2) & Hex33) | |
x = (x + (x >> 4)) & Hex0F | |
(x * Hex01) >> 56 | |
} | |
def time[R](block: => R): (R, Long) = { | |
val t0 = System.currentTimeMillis() | |
val result = block | |
val t1 = System.currentTimeMillis() | |
(result, t1 - t0) | |
} | |
// @ time { (0L to 1000000000).foldLeft(0L)((a,c) => a + countOneBits(c)) } | |
// res98: (Long, Long) = (14846928141L, 31164L) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment