-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
57 lines (48 loc) · 1.05 KB
/
main.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
package main
// #cgo CFLAGS: -g -Wall
// #include <stdlib.h>
// #include "allocate.h"
import "C"
import (
"log"
"unsafe"
"flag"
"time"
)
const SIZE = 1024
const MILLISECONDS = 1000
func main() {
iters := flag.Int("iterations", 1000000, "number of memory allocations iterations")
flag.Parse()
cMalloc(*iters)
cGoMalloc(*iters)
goAssign(*iters)
}
func timeTrack(start time.Time, name string) {
elapsed := time.Since(start);
log.Printf("%s took %10s", name, elapsed);
}
func cMalloc(iterations int) {
iters := C.int(iterations)
duration := float64(C.allocate(iters)) // in seconds
log.Printf("C malloc took %10fms\n", duration * MILLISECONDS)
}
func cGoMalloc(iterations int) {
defer timeTrack(time.Now(), "cGoMalloc")
for i := 0; i < iterations; i++ {
ptr := C.calloc(1 , C.sizeof_char * SIZE)
C.free(unsafe.Pointer(ptr))
}
}
func goAssign(iterations int) {
defer timeTrack(time.Now(), "goAssign")
for i := 0; i < iterations; i++ {
sl := make([]byte, SIZE)
switch {
case 1==1:
// Do nothing
case 1!=1:
log.Printf("%s", sl)
}
}
}