54 lines
1.1 KiB
Markdown
54 lines
1.1 KiB
Markdown
|
# Go Fast Replace
|
|||
|
|
|||
|
An implementation in Go.
|
|||
|
|
|||
|
## To build
|
|||
|
|
|||
|
````bash
|
|||
|
make build
|
|||
|
````
|
|||
|
|
|||
|
## To run
|
|||
|
|
|||
|
````bash
|
|||
|
make run
|
|||
|
````
|
|||
|
|
|||
|
## To test
|
|||
|
|
|||
|
````bash
|
|||
|
make test
|
|||
|
````
|
|||
|
|
|||
|
## Conclusions
|
|||
|
|
|||
|
### Replace
|
|||
|
|
|||
|
Current implementations:
|
|||
|
|
|||
|
- `bm` uses `strings.NewReplacer` which uses the [Boyer–Moore](https://go.dev/src/strings/search.go) algorithm.
|
|||
|
- `cw` uses [Commentz–Walter](https://en.wikipedia.org/wiki/Commentz-Walter_algorithm) algorithm.
|
|||
|
|
|||
|
#### Results
|
|||
|
|
|||
|
- `bm` varies a lot, but is never over _460µs_.
|
|||
|
- `cw` is a lot slower, at around _3.5ms_, but it can search for multiple patterns at once.
|
|||
|
|
|||
|
````shell
|
|||
|
$ make run
|
|||
|
./bin/corpus
|
|||
|
Creating corpus...
|
|||
|
./bin/pairs
|
|||
|
Creating pairs...
|
|||
|
./bin/replace
|
|||
|
|
|||
|
bm: 273.621µs
|
|||
|
|
|||
|
cw: 2.939254ms
|
|||
|
````
|
|||
|
|
|||
|
#### Todo:
|
|||
|
- Implement parallelism on the Commentz–Walter implementation.
|
|||
|
- [Boyer–Moore variants](https://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string-search_algorithm#Variants)
|
|||
|
- [Rabin–Karp](https://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm) algorithm.
|
|||
|
- [Aho–Corasick](https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm) algorithm.
|