Skip to content
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

why TEN times slower than strings.contains #2

Closed
XxYyKk opened this issue Nov 8, 2018 · 1 comment
Closed

why TEN times slower than strings.contains #2

XxYyKk opened this issue Nov 8, 2018 · 1 comment

Comments

@XxYyKk
Copy link

XxYyKk commented Nov 8, 2018

contains( 100001 times) * str len( 1193 ) run time:
482.834µs
kmp-containedIn( 100001 times) * str len( 1193 ) run time:
376.550657ms

strings.contains:

        str := "735224100,1152888111,1112227112,1113337982,111777642..."
        var temp bool // prevent compiler optimization

        //strings.containsIn
        containsCount := 1
	containsRunTime := time.Now()
CONTAINSLOOP:
	if strings.Contains("111111111", str) {
		temp = true
	}

	containsCount++
	if containsCount <= MaxLoop {
		goto CONTAINSLOOP
	}

	fmt.Println("contains(", MaxLoop, "times) * str len(", len(slice), ") run time:\n", time.Since(containsRunTime))

        //kmp.containsIn

        kmpCount := 1
	kmpRunTime := time.Now()
	kmp, _ := NewKMP("1111111111")
KMPLOOP:
	if kmp.ContainedIn(str) {
		temp = true
	}

	kmpCount++
	if kmpCount <= MaxLoop {
		goto KMPLOOP
	}
	fmt.Println("kmp-containedIn(", MaxLoop, "times) * str len(", len(slice), ") run time:\n", time.Since(kmpRunTime))
@paddie
Copy link
Owner

paddie commented Jul 12, 2019

Admittedly, I wrote this a very long time ago.

Most likely the compiler has caught up with any of the benefits that arose from this library when I initially wrote it for a project I was working on. The project was also mainly to understand the KMP algorithm and to see if I could implement it somewhat nicely in Go.

For performance I'd definitely look elsewhere.

@paddie paddie closed this as completed in 5097de8 Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants