From 0c7ba6d3ac8574b2dbacdd96a30709a950df17b1 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sat, 19 May 2018 16:10:38 +0800 Subject: [PATCH] optimization --- ants.go | 5 +---- ants_benchmark_test.go | 14 +++++++++----- pool.go | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/ants.go b/ants.go index 5c2248f..2968a96 100644 --- a/ants.go +++ b/ants.go @@ -1,8 +1,6 @@ package ants -import "math" - -const DEFAULT_POOL_SIZE = math.MaxInt32 +const DEFAULT_POOL_SIZE = 1000 var defaultPool = NewPool(DEFAULT_POOL_SIZE) @@ -21,4 +19,3 @@ func Cap() int { func Free() int { return defaultPool.Free() } - diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 1ce2c52..2f65def 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -5,16 +5,20 @@ import ( "github.com/panjf2000/ants" ) -var size = 1000 - func BenchmarkPoolGroutine(b *testing.B) { - p := ants.NewPool(size) - b.ResetTimer() for i := 0; i < b.N; i++ { - p.Push(demoFunc) + ants.Push(demoFunc) } } +//func BenchmarkPoolGroutine(b *testing.B) { +// p := ants.NewPool(size) +// b.ResetTimer() +// for i := 0; i < b.N; i++ { +// p.Push(demoFunc) +// } +//} + func BenchmarkGoroutine(b *testing.B) { for i := 0; i < b.N; i++ { go demoFunc() diff --git a/pool.go b/pool.go index d058817..3b76159 100644 --- a/pool.go +++ b/pool.go @@ -22,7 +22,7 @@ type Pool struct { func NewPool(size int) *Pool { p := &Pool{ capacity: int32(size), - tasks: make(chan f, 1000), + tasks: make(chan f, size), //workers: &sync.Pool{New: func() interface{} { return &Worker{} }}, workers: make(chan *Worker, size), destroy: make(chan sig, runtime.GOMAXPROCS(-1)),