github.com/scottcagno/storage@v1.8.0/cmd/search/main.go (about)

     1  package main
     2  
     3  import (
     4  	_ "embed"
     5  	"fmt"
     6  	"github.com/scottcagno/storage/pkg/search"
     7  	"time"
     8  )
     9  
    10  var (
    11  	pattern = []string{
    12  		`I do not say these things for a dollar or to fill up the time while I wait for a boat`,
    13  		`pocketless`,
    14  		`baz_DOES_NOT_EXIST`,
    15  		`There is that in me—I do not know what it is—but I know it is in me`,
    16  		`I ascend to the foretruck`,
    17  		`bar_DOES_NOT_EXIST`,
    18  		`Waiting in gloom, protected by frost`,
    19  		`With the twirl of my tongue I encompass worlds and volumes of worlds`,
    20  		`The pleasures of heaven are with me and the pains of hell are with me`,
    21  		`eyes that have shed tears`,
    22  		`Undrape!`,
    23  		`foo_DOES_NOT_EXIST`,
    24  	}
    25  
    26  	//go:embed ww.txt
    27  	text string
    28  )
    29  
    30  func main() {
    31  
    32  	// check out boyer-moore
    33  	bm := search.NewBoyerMoore()
    34  	TimeSearcher(bm)
    35  
    36  	fmt.Println()
    37  
    38  	// check out rabin-karp
    39  	rk := search.NewRabinKarp()
    40  	TimeSearcher(rk)
    41  
    42  	fmt.Println()
    43  
    44  	// check out knuth-morris-pratt
    45  	kmp := search.NewKnuthMorrisPratt()
    46  	TimeSearcher(kmp)
    47  }
    48  
    49  func TimeSearcher(s search.Searcher) {
    50  	fmt.Printf("%s\n", s)
    51  	t1 := time.Now()
    52  	for i := range pattern {
    53  		t3 := time.Now()
    54  		n := s.FindIndexString(text, pattern[i])
    55  		t4 := time.Since(t3)
    56  		fmt.Printf("Found %q, at index %d (Took %.6fs)\n", pattern[i], n, t4.Seconds())
    57  	}
    58  	t2 := time.Since(t1)
    59  	fmt.Printf("Took %.6fs, %dns\n", t2.Seconds(), t2.Nanoseconds())
    60  }
    61  
    62  var sonnet55 = `Not marble nor the gilded monuments
    63  Of princes shall outlive this powerful rhyme;
    64  But you shall shine more bright in these contents
    65  Than unswept stone, besmear'd with sluttish time.
    66  When wasteful war shall statues overturn,
    67  And broils root out the work of masonry,
    68  Nor Mars his sword nor war's quick fire shall burn
    69  The living record of your memory.
    70  'Gainst death and all-oblivious enmity
    71  Shall you pace forth; your praise shall still find room,
    72  Even in the eyes of all posterity
    73  That wear this world out to the ending doom.
    74      So, till the judgment that yourself arise,
    75      You live in this, and dwell in lovers' eyes.`