Catalogue
Simple Performance Measurement in Golang

Simple Performance Measurement in Golang

🌐 日本語で読む

Overview

A quick note on simple performance measurement.
I use it often, so I’m saving it here as a memo.

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
package main

import (
"fmt"
"runtime"
"time"
)

func main() {
// Number of CPUs
cpus := runtime.NumCPU()

// Memory at start
var startMemory runtime.MemStats
runtime.ReadMemStats(&startMemory)

// Start time
start := time.Now()


// do something


// Elapsed time
elapsed := time.Since(start)

// Memory at end
var endMemory runtime.MemStats
runtime.ReadMemStats(&endMemory)

fmt.Printf("実行時間: %f 秒 \n", elapsed.Seconds())
fmt.Printf("CPU: %d \n", cpus)
fmt.Printf("Memory All: %f MB \n", float64(endMemory.Alloc-startMemory.Alloc)/float64(1024*1024))
}
Author

Kenzo Tanaka

Posted on

2016-11-21

Licensed under