forked from fnproject/fn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.go
58 lines (53 loc) · 1.33 KB
/
func.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"
)
// return list of primes less than N
// source: http://stackoverflow.com/a/21923233
func sieveOfEratosthenes(N int) (primes []int) {
b := make([]bool, N)
for i := 2; i < N; i++ {
if b[i] == true {
continue
}
primes = append(primes, i)
for k := i * i; k < N; k += i {
b[k] = true
}
}
return
}
func main() {
start := time.Now()
maxPrime := 1000000
numLoops := 1
// Parse the query string
s := strings.Split(os.Getenv("FN_REQUEST_URL"), "?")
if len(s) > 1 {
for _, pair := range strings.Split(s[1], "&") {
kv := strings.Split(pair, "=")
if len(kv) > 1 {
key, value := kv[0], kv[1]
if key == "max" {
maxPrime, _ = strconv.Atoi(value)
}
if key == "loops" {
numLoops, _ = strconv.Atoi(value)
}
}
}
}
// Repeat the calculation of primes simply to give the CPU more work to do without consuming additional memory
for i := 0; i < numLoops; i++ {
primes := sieveOfEratosthenes(maxPrime)
_ = primes
if i == numLoops-1 {
//fmt.Printf("Highest three primes: %d %d %d\n", primes[len(primes) - 1], primes[len(primes) - 2], primes[len(primes) - 3])
}
}
fmt.Printf("{\"durationSeconds\": %f, \"hostname\": \"%s\", \"max\": %d, \"loops\": %d}", time.Since(start).Seconds(), os.Getenv("HOSTNAME"), maxPrime, numLoops)
}