Skip to content

Instantly share code, notes, and snippets.

@starius
Created July 25, 2017 15:22
Show Gist options
  • Save starius/5d0a73abee1b42156001e62d9140d14b to your computer and use it in GitHub Desktop.
Save starius/5d0a73abee1b42156001e62d9140d14b to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
"time"
)
const n = 1000000000
var m sync.Mutex
func withLambda() {
m.Lock()
defer func() { m.Unlock() }()
}
func withoutLambda() {
m.Lock()
defer m.Unlock()
}
func main() {
t1 := time.Now()
for i := 0; i < n; i++ {
withLambda()
}
fmt.Printf("With lambda: %f ns\n", float64(time.Since(t1).Nanoseconds()) / n)
//
t2 := time.Now()
for i := 0; i < n; i++ {
withoutLambda()
}
fmt.Printf("Without lambda: %f ns\n", float64(time.Since(t2).Nanoseconds()) / n)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment