-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopcount.go
83 lines (76 loc) · 1.94 KB
/
popcount.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/******************************************************************************
* Compilation: go build popcount.go
* Execution: ./popcount 123456
*
* PopCount returns the population count (number of set bits) of x.
*
* % ./popcount 123456
* result is 6 for 123456 from PopCount
* result is 6 for 123456 from PopCountByClear
* a benchmark for measuring two PopCount method’s performance
* 49.86ms elapsed for PopCount
* 4557.68ms elapsed for PopCountByClear
*
******************************************************************************/
package main
import (
"fmt"
"math/rand"
"os"
"strconv"
"time"
)
var pc [256]byte
var data [100000000]uint64
func init() {
for i := range pc {
pc[i] = pc[i/2] + byte(i&1)
}
}
func init() {
for i := range data {
data[i] = rand.Uint64()
}
}
// PopCount return the population count (number of set bits) of x
func PopCount(x uint64) int {
return int(pc[byte(x>>(0*8))] +
pc[byte(x>>(1*8))] +
pc[byte(x>>(2*8))] +
pc[byte(x>>(3*8))] +
pc[byte(x>>(4*8))] +
pc[byte(x>>(5*8))] +
pc[byte(x>>(6*8))] +
pc[byte(x>>(7*8))])
}
// PopCountByClear using clear rightmost bit
func PopCountByClear(x uint64) int {
n := 0
for x != 0 {
x = x & (x - 1) // clear rightmost non-zero bit
n++
}
return n
}
func main() {
if v, err := strconv.ParseUint(os.Args[1], 10, 64); err == nil {
fmt.Printf("result is %d for %d from PopCount\n", PopCount(v), v)
fmt.Printf("result is %d for %d from PopCountByClear\n",
PopCountByClear(v), v)
} else {
fmt.Fprintf(os.Stderr, "PopCount: %v\n", err)
os.Exit(1)
}
fmt.Println("a benchmark for measuring two PopCount method’s performance")
start := time.Now()
for i := range data {
PopCount(data[i])
}
fmt.Printf("%.2fms elapsed for PopCount\n", time.Since(start).Seconds()*1000)
start = time.Now()
for i := range data {
PopCountByClear(data[i])
}
fmt.Printf("%.2fms elapsed for PopCountByClear\n",
time.Since(start).Seconds()*1000)
}