gokmp
=====
String-matching in Golang using the Knuth–Morris–Pratt algorithm (KMP).
## Disclaimer
This library was written as part of my Master's Thesis and should be used as a helpful implementation reference for people interested in the Knuth-Morris-Pratt algorithm than as a performance string searching library.
I believe the compiler has since caught up to most of the gains that this library bought me back in the day.
Example:
========
```Go
package main
import (
"fmt"
"github.com/paddie/gokmp"
)
const str = "aabaabaaaabbaabaabaaabbaabaabb"
// " _ _ _ "
// 8 19 26
const pattern = "aabb"
func main() {
kmp, _ := gokmp.NewKMP(pattern)
ints := kmp.FindAllStringIndex(str)
fmt.Println(ints)
}
```
Output:
=======
```Go
[8 19 26]
```
Tests and Benchmarks:
=====================
```
go test -v -bench .
```
Output:
=======
```
=== RUN TestFindAllStringIndex
--- PASS: TestFindAllStringIndex (0.00 seconds)
=== RUN TestFindStringIndex
--- PASS: TestFindStringIndex (0.00 seconds)
=== RUN TestContainedIn
--- PASS: TestContainedIn (0.00 seconds)
=== RUN TestOccurrences
--- PASS: TestOccurrences (0.00 seconds)
=== RUN TestOccurrencesFail
--- PASS: TestOccurrencesFail (0.00 seconds)
PASS
BenchmarkKMPIndexComparison 10000000 178 ns/op
BenchmarkStringsIndexComparison 10000000 359 ns/op
ok github.com/paddie/gokmp 5.854s
```
Comparison:
============
```bash
gokmp.FindStringIndex / strings.Index = 178/359 = 0.4958
```
Almost a 2x improvement over the naive built-in method.
kmp算法-使用kmp算法在Golang中实现字符串匹配.zip
需积分: 1 23 浏览量
2024-05-16
06:08:08
上传
评论
收藏 3KB ZIP 举报
m0_57195758
- 粉丝: 1202
- 资源: 372
最新资源
- Anaconda3-2024.02-1-Windows-x86-64.exe
- stm32cubemx can通信 一个板子负责传感器采集 一个板子负责wifi传递到网页 网页可控制传感器板子.zip
- k8s集群搭建1.27.1版本(来源于图灵k8s笔记)
- 树莓派连接 DS18B20 温度传感器读取温度时监控客户端,服务器端连接状况,实现断线重连机制
- 小游戏-坦克大战,你认为的小游戏
- 最好用的富文本编辑器wangeditor
- jQuery 3.7.1
- 校园失物招领小程序源码可作毕业设计
- SAP客户端GUI740安装包(JAVA版本)
- winlibs-x86-64-posix-seh-gcc-13.2.0-llvm-16.0.6-mingw-w64msvcrt
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈