Code Monkey home page Code Monkey logo

study-go-multi-writer's Introduction

io.MultiWriter は遅いから、使わなくてもよい時は使わない方がよいというだけのリポート

string とファイル(ここでは /dev/null)へ同じものを出力する

package multi

import (
    "bufio"
    "io"
    "strings"
)

// Multi1 - strings.Builder で string のインスタンスを作ったあと、それをファイルにも出力する
func Multi1(n int, w io.Writer) string {
    var buffer strings.Builder
    for i := 0; i < n; i++ {
        buffer.WriteByte(byte(i))
    }
    s := buffer.String()
    io.WriteString(w, s)
    return s
}

// Multi2 - MultiWriter で、strings.Builder とファイルに同時出力
func Multi2(n int, w io.Writer) string {
    var buffer strings.Builder
    ww := io.MultiWriter(w, &buffer)
    for i := 0; i < n; i++ {
        ww.Write([]byte{byte(i)})
    }
    return buffer.String()
}

// Multi3 - Multi2 と同じだが、bufio でオーバーヘッドを軽減
func Multi3(n int, w io.Writer) string {
    var buffer strings.Builder
    bw := bufio.NewWriter(io.MultiWriter(w, &buffer))
    for i := 0; i < n; i++ {
        bw.WriteByte(byte(i))
    }
    bw.Flush()
    return buffer.String()
}
package multi_test

import (
    "io"
    "os"
    "testing"

    "github.com/hymkor/study-go-multi-writer"
)

func try(f func(int, io.Writer) string, n int) {
    devNull, err := os.OpenFile(os.DevNull, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
    if err != nil {
        panic(err.Error())
    }
    defer devNull.Close()

    f(n, devNull)
}

func BenchmarkMulti1(b *testing.B) {
    try(multi.Multi1, b.N)
}

func BenchmarkMulti2(b *testing.B) {
    try(multi.Multi2, b.N)
}

func BenchmarkMulti3(b *testing.B) {
    try(multi.Multi3, b.N)
}

「go test -bench . -benchmem」の結果

goos: windows
goarch: amd64
pkg: github.com/hymkor/study-go-multi-writer
cpu: Intel(R) Core(TM) i5-6500T CPU @ 2.50GHz
BenchmarkMulti1-4   	355637443	         3.080 ns/op	       5 B/op	       0 allocs/op
BenchmarkMulti2-4   	  734821	      1609 ns/op	       6 B/op	       1 allocs/op
BenchmarkMulti3-4   	272213160	         4.154 ns/op	       5 B/op	       0 allocs/op
PASS
ok  	github.com/hymkor/study-go-multi-writer	5.670s

study-go-multi-writer's People

Contributors

hymkor avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

isgasho

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.