Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Celebrating premature optimization
package main
import (
"fmt"
"time"
)
func main() {
var intTrash int
var byteTrash byte
now := time.Now()
for i := 0; i < 100000000; i++ {
var num int
for num = 0; num < 8; num++ {
intTrash = num
}
}
elapsed1 := time.Since(now)
now = time.Now()
for i := 0; i < 100000000; i++ {
var num byte
for num = 0; num < 8; num++ {
byteTrash = num
}
}
elapsed2 := time.Since(now)
fmt.Println("Integers:", elapsed1)
fmt.Println("Bytes:", elapsed2)
fmt.Println(intTrash, byteTrash)
}
Owner

pierrebeaucamp commented Aug 2, 2016

❯ go run main.go
Integers: 893.864892ms
Bytes: 612.049695ms
7 7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment