Skip to content

Instantly share code, notes, and snippets.

@Preetam
Created July 2, 2014 14:24
Show Gist options
  • Save Preetam/81b79569d94813270f33 to your computer and use it in GitHub Desktop.
Save Preetam/81b79569d94813270f33 to your computer and use it in GitHub Desktop.
package main
import (
"sync"
"testing"
)
// BenchmarkPanicRecovery 1000000 1025 ns/op
func BenchmarkPanicRecovery(b *testing.B) {
buffer := []byte{0, 1, 2, 3}
lock := sync.Mutex{}
for i := 0; i < b.N; i++ {
lock.Lock()
go func() {
defer func() {
if r := recover(); r != nil {
}
lock.Unlock()
}()
buffer[5] = 4
}()
}
}
// BenchmarkBoundsCheck 5000000 548 ns/op
func BenchmarkBoundsCheck(b *testing.B) {
buffer := []byte{0, 1, 2, 3}
lock := sync.Mutex{}
for i := 0; i < b.N; i++ {
lock.Lock()
go func() {
defer func() {
if r := recover(); r != nil {
}
lock.Unlock()
}()
if len(buffer) > 5 {
buffer[5] = 4
}
}()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment