Skip to content

Dramatically speed down regexp while grow difficulty it #14167

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rekby opened this issue Jan 31, 2016 · 1 comment
Closed

Dramatically speed down regexp while grow difficulty it #14167

rekby opened this issue Jan 31, 2016 · 1 comment

Comments

@rekby
Copy link

rekby commented Jan 31, 2016

When I try to search some more difficult then simple substring (for example one of two substring) speed of regexp will very very slower.

For emaxple few simple regexps for 10Mbytes empty array

package main

import (
    "fmt"
    "regexp"
    "time"
)
var f []byte

func test(s string) {
    r := regexp.MustCompile(s)
    fmt.Print(s + ": ")
    start := time.Now()
    r.FindAllIndex(f, -1)
    fmt.Println(time.Now().Sub(start))

}

func main() {
    f = make([]byte, 10*1024*1024)
    test("aaa")
    test("bbb")
    test("aaa.*")
    test("aaa.*bbb")
    test("aaa|bbb")
    test("aaa|bbb|ccc")
}

Output for my laptop:

aaa: 2.265268ms
bbb: 1.154115ms
aaa.*: 1.053976ms
aaa.*bbb: 995.438µs
aaa|bbb: 1.675626998s
aaa|bbb|ccc: 2.518271136s
@bradfitz
Copy link
Contributor

bradfitz commented Feb 1, 2016

It's true that some regexps are faster than others. But they all execute in linear time with the length of the string being matched.

This bug is too general to be actionable. There are more specific regexp performance bugs open, such as #11646.

@bradfitz bradfitz closed this as completed Feb 1, 2016
@golang golang locked and limited conversation to collaborators Feb 3, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants