Created
August 18, 2019 22:10
-
-
Save caesaneer/2dd7fde3876567f90fbc1420a94c5689 to your computer and use it in GitHub Desktop.
Golang - Single and 16 CPU Threads - Bench Package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package bench | |
import ( | |
"math" | |
"strconv" | |
) | |
// CPU does stuff | |
func CPU(times int) []string { | |
// Keeps it to a 32 bit int | |
num := 40 | |
var r []string | |
for i := 0; i < times; i++ { | |
d := toString(bubble(expand(reversePrime(fib(num))))) | |
r = append(r, d[len(d)-1]) | |
} | |
return r | |
} | |
func fib(n int) []int { | |
s := []int{1} | |
c := 1 | |
p := 0 | |
i := n - 1 | |
for i >= 1 { | |
c += p | |
p = c - p | |
s = append(s, c) | |
i-- | |
} | |
return s | |
} | |
func prime(n int) bool { | |
if n%1 != 0 { | |
return false | |
} else if n <= 1 { | |
return false | |
} else if n <= 3 { | |
return true | |
} else if n%2 == 0 { | |
return false | |
} | |
dl := int(math.Sqrt(float64(n))) | |
for d := 3; d <= dl; d += 2 { | |
if n%d == 0 { | |
return false | |
} | |
} | |
return true | |
} | |
func reversePrime(slice []int) []int { | |
l := len(slice) - 1 | |
var r []int | |
for l >= 0 { | |
if prime(slice[l]) { | |
r = append(r, slice[l]) | |
} | |
l-- | |
} | |
return r | |
} | |
func expand(slice []int) []int { | |
ol := len(slice) | |
oc := 0 | |
l := ol * 10 | |
var r []int | |
for i := 0; i < l; i++ { | |
r = append(r, (slice[oc] + (i * 100))) | |
if oc < ol-1 { | |
oc++ | |
} else { | |
oc = 0 | |
} | |
} | |
return r | |
} | |
func bubble(slice []int) []int { | |
for i := 0; i < len(slice); i++ { | |
for y := 0; y < len(slice)-1; y++ { | |
if slice[y+1] < slice[y] { | |
t := slice[y] | |
slice[y] = slice[y+1] | |
slice[y+1] = t | |
} | |
} | |
} | |
return slice | |
} | |
func toString(slice []int) []string { | |
var r []string | |
for i := 0; i < len(slice); i++ { | |
r = append(r, strconv.Itoa(slice[i])) | |
} | |
return r | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment