Coder Social home page Coder Social logo

study-join-go's Introduction

study-join-go

The best algorithm to join strings with a separator

【結論】 strings.Join 的アルゴリズム、

    sep := ""
    for i := 0; i < n; i++ {
        buffer.WriteString(sep)
        buffer.WriteString("foo")
        sep = ","
    }

と書くとコンパクトでいいかなと思ったが、ベンチとったら予想以上に遅かった。

main.go

package studyjoin

import (
    "strings"
)

func join1(n int) string {
    var buffer strings.Builder
    buffer.Grow(n * 4)
    sep := ""

    for i := 0; i < n; i++ {
        buffer.WriteString(sep)
        buffer.WriteString("foo")
        sep = ","
    }
    return buffer.String()
}

func join2(n int) string {
    var buffer strings.Builder
    buffer.Grow(n * 4)

    for i := 0; i < n; i++ {
        if i > 0 {
            buffer.WriteString(",")
        }
        buffer.WriteString("foo")
    }
    return buffer.String()
}

func join3(n int) string {
    var buffer strings.Builder
    buffer.Grow(n * 4)

    buffer.WriteString("foo")
    for i := 1; i < n; i++ {
        buffer.WriteString(",")
        buffer.WriteString("foo")
    }
    return buffer.String()
}

func join4(n int) string {
    var buffer strings.Builder
    buffer.Grow(n * 4)

    for i := 0; ; {
        buffer.WriteString("foo")
        i++
        if i >= n {
            break
        }
        buffer.WriteString(",")
    }
    return buffer.String()
}

main_test.go

package studyjoin

import (
    "testing"
)

func Test1(t *testing.T) {
    if join1(3) != "foo,foo,foo" {
        t.Fatal()
    }
}

func Benchmark1(B *testing.B) {
    join1(B.N)
}

func Test2(t *testing.T) {
    if join2(3) != "foo,foo,foo" {
        t.Fatal()
    }
}

func Benchmark2(B *testing.B) {
    join2(B.N)
}

func Test3(t *testing.T) {
    if join3(3) != "foo,foo,foo" {
        t.Fatal()
    }
}

func Benchmark3(B *testing.B) {
    join3(B.N)
}

func Test4(t *testing.T) {
    if join4(3) != "foo,foo,foo" {
        t.Fatal()
    }
}

func Benchmark4(B *testing.B) {
    join4(B.N)
}

Benchmark

go test -bench . -benchmem

goos: windows
goarch: amd64
pkg: github.com/hymkor/study-join
cpu: Intel(R) Core(TM) i5-6500T CPU @ 2.50GHz
Benchmark1-4   	171278196	         6.783 ns/op	       4 B/op	       0 allocs/op
Benchmark2-4   	286658004	         5.637 ns/op	       4 B/op	       0 allocs/op
Benchmark3-4   	289203525	         4.104 ns/op	       4 B/op	       0 allocs/op
Benchmark4-4   	323644233	         3.937 ns/op	       4 B/op	       0 allocs/op
PASS
ok  	github.com/hymkor/study-join	8.575s

study-join-go's People

Contributors

hymkor avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.