Created
July 2, 2014 14:24
-
-
Save Preetam/81b79569d94813270f33 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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