diff --git a/bench/algorithm/coro-prime-sieve/2.go b/bench/algorithm/coro-prime-sieve/2.go new file mode 100644 index 00000000..71d772f1 --- /dev/null +++ b/bench/algorithm/coro-prime-sieve/2.go @@ -0,0 +1,61 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "os" + "strconv" +) + +// get r3kt. +// - yung innanet + +func SieveOfEratosthenes(out io.Writer) { + sieve := make([]bool, 20000) + p := 2 + + for { + if !sieve[p] { + _, _ = fmt.Fprintln(out, p) + for multiple := p * p; multiple <= len(sieve)-1; multiple += p { + sieve[multiple] = true + } + } + p++ + if p > len(sieve)-1 { + return + } + } +} + +func main() { + n := 1000 + if len(os.Args) > 1 { + if _n, err := strconv.Atoi(os.Args[1]); err == nil { + n = _n + } + } + pipeIn, pipeOut := io.Pipe() + + out := bufio.NewWriter(os.Stdout) + in := bufio.NewWriter(pipeOut) + + readerUntil := bufio.NewReader(pipeIn) + go SieveOfEratosthenes(in) + + rd := func() []byte { + b, _ := readerUntil.ReadSlice('\n') + return b + } + + i := 0 + for { + if i >= n { + break + } + _, _ = out.Write(rd()) + i++ + } + _ = out.Flush() +} diff --git a/bench/bench_go.yaml b/bench/bench_go.yaml index 34379247..4ef60979 100644 --- a/bench/bench_go.yaml +++ b/bench/bench_go.yaml @@ -45,6 +45,7 @@ problems: - name: coro-prime-sieve source: - 1.go + - 2.go - name: http-server source: - 1.go diff --git a/bench/bench_go_tinygo.yaml b/bench/bench_go_tinygo.yaml index 75bc7a07..6dce70b9 100644 --- a/bench/bench_go_tinygo.yaml +++ b/bench/bench_go_tinygo.yaml @@ -40,6 +40,7 @@ problems: - name: coro-prime-sieve source: - 1.go + - 2.go # - name: json-serde # source: # - 1.go