Skip to content

Commit

Permalink
Golang: obliterate coro-prime-sieve score
Browse files Browse the repository at this point in the history
  • Loading branch information
yunginnanet committed Jun 22, 2024
1 parent f2172e6 commit 0899bd2
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
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

0 comments on commit 0899bd2

Please sign in to comment.