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

Go: improved coro-sine-sieve (add 2.go) #439

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions bench/algorithm/coro-prime-sieve/2.go
Original file line number Diff line number Diff line change
@@ -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()
}
1 change: 1 addition & 0 deletions bench/bench_go.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ problems:
- name: coro-prime-sieve
source:
- 1.go
- 2.go
- name: http-server
source:
- 1.go
Expand Down
1 change: 1 addition & 0 deletions bench/bench_go_tinygo.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ problems:
- name: coro-prime-sieve
source:
- 1.go
- 2.go
# - name: json-serde
# source:
# - 1.go
Expand Down