File: sieve_bench_test.go

package info (click to toggle)
golang-github-opencoff-go-sieve 0.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 108 kB
  • sloc: makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,380 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// sieve_bench_test.go -- benchmark testing
//
// (c) 2024 Sudhi Herle <sudhi@herle.net>
//
// Copyright 2024- Sudhi Herle <sw-at-herle-dot-net>
// License: BSD-2-Clause
//
// If you need a commercial license for this work, please contact
// the author.
//
// This software does not come with any express or implied
// warranty; it is provided "as is". No claim  is made to its
// suitability for any purpose.

package sieve_test

import (
	"math/rand"
	"sync/atomic"
	"testing"

	"github.com/opencoff/go-sieve"
)

func BenchmarkSieve_Add(b *testing.B) {
	c := sieve.New[int, int](8192)
	ent := make([]int, b.N)

	for i := 0; i < b.N; i++ {
		var k int
		if i%2 == 0 {
			k = int(rand.Int63() % 16384)
		} else {
			k = int(rand.Int63() % 32768)
		}
		ent[i] = k
	}

	b.ResetTimer()
	for i := 0; i < b.N; i++ {
		k := ent[i]
		c.Add(k, k)
	}
}

func BenchmarkSieve_Get(b *testing.B) {
	c := sieve.New[int, int](8192)
	ent := make([]int, b.N)
	for i := 0; i < b.N; i++ {
		var k int
		if i%2 == 0 {
			k = int(rand.Int63() % 16384)
		} else {
			k = int(rand.Int63() % 32768)
		}
		c.Add(k, k)
		ent[i] = k
	}

	b.ResetTimer()

	var hit, miss int64
	for i := 0; i < b.N; i++ {
		if _, ok := c.Get(ent[i]); ok {
			atomic.AddInt64(&hit, 1)
		} else {
			atomic.AddInt64(&miss, 1)
		}
	}

	b.Logf("%d: hit %d, miss %d, ratio %4.2f", b.N, hit, miss, float64(hit)/float64(hit+miss))
}