Your benchmark code is broken, try

func BenchmarkAdd(b *testing.B) {
    q = New(100000)
    for _ = range b.N {
        q.Add(1)
    }
}

which reports:

Running tool: /usr/local/bin/go test -benchmem -run=^$ -bench ^BenchmarkAdd$ 
example.com/queue

goos: darwin
goarch: amd64
pkg: example.com/queue
cpu: Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
=== RUN   BenchmarkAdd
BenchmarkAdd
BenchmarkAdd-8          25188489                47.18 ns/op           16 B/op   
       1 allocs/op
PASS
ok      example.com/queue       1.363s

> On Jul 26, 2025, at 4:18 AM, karthick ayyapillai (கார்த்திக்) 
> <a.kart...@gmail.com> wrote:
> 
> hi,
> I am using a simple queue and bench marking it.
> queue.go = implementation of the queue.
> queue_test.go = benchmarking
> profile002.pdf= pprof tool mem analysis
> the command used to run the bench mark is
> `go test -run='^#' -bench=BenchmarkAdd -benchmem -count=10`
> 
> results :
> ```
> go test -run='^#' -bench=BenchmarkAdd -benchmem -count=10
> goos: linux
> goarch: amd64
> pkg: queue-middleware/queue
> cpu: Intel(R) Core(TM) i7-10510U CPU @ 1.80GHz
> BenchmarkAdd-8          78255547                15.13 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          86375668                13.84 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          91471299                13.00 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          87612523                12.70 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          93454760                12.76 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          91850235                12.75 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          97128108                13.36 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          93765214                12.94 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          86242954                13.07 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> BenchmarkAdd-8          88524364                12.76 ns/op            0 B/op 
>          0 allocs/op
> --- BENCH: BenchmarkAdd-8
>     queue_test.go:36: size:1
>     queue_test.go:36: size:5
> PASS
> ok      queue-middleware/queue  11.831s
> ```
> my understanding is 
> - 0B/op = no allocation done
> - 0 allocs/op = no allocation per operation
> 
> Does this mean the allocation is zero for the `Add` operation?
> 
> generally,  when you create queue it allocates memory in heap, will it not be 
> reported in `benchmem`?
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to golang-nuts+unsubscr...@googlegroups.com 
> <mailto:golang-nuts+unsubscr...@googlegroups.com>.
> To view this discussion visit 
> https://groups.google.com/d/msgid/golang-nuts/42c18d81-66bc-4616-9817-77195bbadeb3n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/golang-nuts/42c18d81-66bc-4616-9817-77195bbadeb3n%40googlegroups.com?utm_medium=email&utm_source=footer>.
> <profile002.pdf><queue.go><queue_test.go>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/F53B6765-53C0-4060-BAF6-74E49D8B991E%40ix.netcom.com.

Reply via email to