Skip to content

Instantly share code, notes, and snippets.

@pierrebeaucamp
Created August 2, 2016 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pierrebeaucamp/d01b71e28bc6cdf53ff8404a98869260 to your computer and use it in GitHub Desktop.
Save pierrebeaucamp/d01b71e28bc6cdf53ff8404a98869260 to your computer and use it in GitHub Desktop.
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)
}
@pierrebeaucamp
Copy link
Author

❯ 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