// Item the type of the stack type Item interface{}
// ItemStack the stack of Items type ItemStack struct { items []Item lock sync.RWMutex }
// New creates a new ItemStack func NewStack() *ItemStack { s := &ItemStack{} s.items = []Item{} return s }
// Pirnt prints all the elements func (s *ItemStack) Print() { fmt.Println(s.items) }
// Push adds an Item to the top of the stack func (s *ItemStack) Push(t Item) { s.lock.Lock() s.lock.Unlock() s.items = append(s.items, t) }
// Pop removes an Item from the top of the stack func (s *ItemStack) Pop() Item { s.lock.Lock() defer s.lock.Unlock() if len(s.items) == 0 { return nil } item := s.items[len(s.items)-1] s.items = s.items[0 : len(s.items)-1] return item }
func Benchmark_Push(b *testing.B) { for i := 0; i < b.N; i++ { //use b.N for looping stack.Push("test") } }
func Benchmark_Pop(b *testing.B) { b.StopTimer() for i := 0; i < b.N; i++ { //use b.N for looping stack.Push("test") } b.StartTimer() for i := 0; i < b.N; i++ { //use b.N for looping stack.Pop() } }
1 2 3 4 5 6 7 8
$ go test -test.bench=".*" -benchmem -v goos: darwin goarch: amd64 pkg: test/test8 Benchmark_Push-410000000222 ns/op 94 B/op 0 allocs/op Benchmark_Pop-42000000065.0 ns/op 0 B/op 0 allocs/op PASS ok test/test8 7.644s
type ( Stack struct { top *node length int lock *sync.RWMutex } node struct { value interface{} prev *node } )
// Create a new stack func NewStack() *Stack { return &Stack{nil, 0, &sync.RWMutex{}} }
// Return the number of items in the stack func (this *Stack) Len() int { return this.length }
// View the top item on the stack func (this *Stack) Peek() interface{} { if this.length == 0 { return nil } return this.top.value }
// Pop the top item of the stack and return it func (this *Stack) Pop() interface{} { this.lock.Lock() defer this.lock.Unlock() if this.length == 0 { return nil } n := this.top this.top = n.prev this.length-- return n.value }
// Push a value onto the top of the stack func (this *Stack) Push(value interface{}) { this.lock.Lock() defer this.lock.Unlock() n := &node{value, this.top} this.top = n this.length++ }
// Item the type of the stack type Item interface{}
// ItemStack the stack of Items type ItemStack struct { items []Item lock sync.RWMutex }
// New creates a new ItemStack func NewStack() *ItemStack { s := &ItemStack{} s.items = []Item{} return s }
// Pirnt prints all the elements func (s *ItemStack) Print() { fmt.Println(s.items) }
// Push adds an Item to the top of the stack func (s *ItemStack) Push(t Item) { s.lock.Lock() s.lock.Unlock() s.items = append(s.items, t) }
// Pop removes an Item from the top of the stack func (s *ItemStack) Pop() Item { s.lock.Lock() defer s.lock.Unlock() if len(s.items) == 0 { return nil } item := s.items[len(s.items)-1] s.items = s.items[0 : len(s.items)-1] return item }
func Benchmark_Push(b *testing.B) { for i := 0; i < b.N; i++ { //use b.N for looping stack.Push("test") } }
func Benchmark_Pop(b *testing.B) { b.StopTimer() for i := 0; i < b.N; i++ { //use b.N for looping stack.Push("test") } b.StartTimer() for i := 0; i < b.N; i++ { //use b.N for looping stack.Pop() } }
1 2 3 4 5 6 7 8
$ go test -test.bench=".*" -benchmem -v goos: darwin goarch: amd64 pkg: test/test8 Benchmark_Push-410000000222 ns/op 94 B/op 0 allocs/op Benchmark_Pop-42000000065.0 ns/op 0 B/op 0 allocs/op PASS ok test/test8 7.644s
type ( Stack struct { top *node length int lock *sync.RWMutex } node struct { value interface{} prev *node } )
// Create a new stack func NewStack() *Stack { return &Stack{nil, 0, &sync.RWMutex{}} }
// Return the number of items in the stack func (this *Stack) Len() int { return this.length }
// View the top item on the stack func (this *Stack) Peek() interface{} { if this.length == 0 { return nil } return this.top.value }
// Pop the top item of the stack and return it func (this *Stack) Pop() interface{} { this.lock.Lock() defer this.lock.Unlock() if this.length == 0 { return nil } n := this.top this.top = n.prev this.length-- return n.value }
// Push a value onto the top of the stack func (this *Stack) Push(value interface{}) { this.lock.Lock() defer this.lock.Unlock() n := &node{value, this.top} this.top = n this.length++ }