summaryrefslogtreecommitdiff
path: root/test/bench
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2009-12-15 15:41:46 -0800
committerRobert Griesemer <gri@golang.org>2009-12-15 15:41:46 -0800
commit441b088d9ec0fb268a5ffa0777fb0c57f4cf557f (patch)
tree274d1d9bf832b7834ab60c65acdf945576271d14 /test/bench
parente84d83bb173b2a296629072b2b50ef86e0746de3 (diff)
downloadgo-441b088d9ec0fb268a5ffa0777fb0c57f4cf557f.tar.gz
1) Change default gofmt default settings for
parsing and printing to new syntax. Use -oldparser to parse the old syntax, use -oldprinter to print the old syntax. 2) Change default gofmt formatting settings to use tabs for indentation only and to use spaces for alignment. This will make the code alignment insensitive to an editor's tabwidth. Use -spaces=false to use tabs for alignment. 3) Manually changed src/exp/parser/parser_test.go so that it doesn't try to parse the parser's source files using the old syntax (they have new syntax now). 4) gofmt -w src misc test/bench 5th and last set of files. R=rsc CC=golang-dev http://codereview.appspot.com/180050
Diffstat (limited to 'test/bench')
-rw-r--r--test/bench/binary-tree-freelist.go66
-rw-r--r--test/bench/binary-tree.go36
-rw-r--r--test/bench/chameneosredux.go96
-rw-r--r--test/bench/fannkuch.go52
-rw-r--r--test/bench/fasta.go88
-rw-r--r--test/bench/k-nucleotide.go90
-rw-r--r--test/bench/mandelbrot.go62
-rw-r--r--test/bench/meteor-contest.go242
-rw-r--r--test/bench/nbody.go120
-rw-r--r--test/bench/pidigits.go66
-rw-r--r--test/bench/regex-dna.go38
-rw-r--r--test/bench/reverse-complement.go44
-rw-r--r--test/bench/spectral-norm-parallel.go48
-rw-r--r--test/bench/spectral-norm.go38
-rw-r--r--test/bench/threadring.go30
15 files changed, 558 insertions, 558 deletions
diff --git a/test/bench/binary-tree-freelist.go b/test/bench/binary-tree-freelist.go
index 21fe5c5d9..071a4e06e 100644
--- a/test/bench/binary-tree-freelist.go
+++ b/test/bench/binary-tree-freelist.go
@@ -37,19 +37,19 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
+ "flag"
+ "fmt"
)
var n = flag.Int("n", 15, "depth")
type Node struct {
- item int;
- left, right *Node;
+ item int
+ left, right *Node
}
type Arena struct {
- head *Node;
+ head *Node
}
var arena Arena
@@ -61,69 +61,69 @@ func (n *Node) free() {
if n.right != nil {
n.right.free()
}
- n.left = arena.head;
- arena.head = n;
+ n.left = arena.head
+ arena.head = n
}
func (a *Arena) New(item int, left, right *Node) *Node {
if a.head == nil {
- nodes := make([]Node, 3<<uint(*n));
+ nodes := make([]Node, 3<<uint(*n))
for i := 0; i < len(nodes)-1; i++ {
nodes[i].left = &nodes[i+1]
}
- a.head = &nodes[0];
+ a.head = &nodes[0]
}
- n := a.head;
- a.head = a.head.left;
- n.item = item;
- n.left = left;
- n.right = right;
- return n;
+ n := a.head
+ a.head = a.head.left
+ n.item = item
+ n.left = left
+ n.right = right
+ return n
}
func bottomUpTree(item, depth int) *Node {
if depth <= 0 {
return arena.New(item, nil, nil)
}
- return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1));
+ return arena.New(item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1))
}
func (n *Node) itemCheck() int {
if n.left == nil {
return n.item
}
- return n.item + n.left.itemCheck() - n.right.itemCheck();
+ return n.item + n.left.itemCheck() - n.right.itemCheck()
}
const minDepth = 4
func main() {
- flag.Parse();
+ flag.Parse()
- maxDepth := *n;
+ maxDepth := *n
if minDepth+2 > *n {
maxDepth = minDepth + 2
}
- stretchDepth := maxDepth + 1;
+ stretchDepth := maxDepth + 1
- check := bottomUpTree(0, stretchDepth).itemCheck();
- fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check);
+ check := bottomUpTree(0, stretchDepth).itemCheck()
+ fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check)
- longLivedTree := bottomUpTree(0, maxDepth);
+ longLivedTree := bottomUpTree(0, maxDepth)
for depth := minDepth; depth <= maxDepth; depth += 2 {
- iterations := 1 << uint(maxDepth-depth+minDepth);
- check = 0;
+ iterations := 1 << uint(maxDepth-depth+minDepth)
+ check = 0
for i := 1; i <= iterations; i++ {
- t := bottomUpTree(i, depth);
- check += t.itemCheck();
- t.free();
- t = bottomUpTree(-i, depth);
- check += t.itemCheck();
- t.free();
+ t := bottomUpTree(i, depth)
+ check += t.itemCheck()
+ t.free()
+ t = bottomUpTree(-i, depth)
+ check += t.itemCheck()
+ t.free()
}
- fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check);
+ fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check)
}
- fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth, longLivedTree.itemCheck());
+ fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth, longLivedTree.itemCheck())
}
diff --git a/test/bench/binary-tree.go b/test/bench/binary-tree.go
index 88497d490..9f867d11a 100644
--- a/test/bench/binary-tree.go
+++ b/test/bench/binary-tree.go
@@ -37,56 +37,56 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
+ "flag"
+ "fmt"
)
var n = flag.Int("n", 15, "depth")
type Node struct {
- item int;
- left, right *Node;
+ item int
+ left, right *Node
}
func bottomUpTree(item, depth int) *Node {
if depth <= 0 {
return &Node{item: item}
}
- return &Node{item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1)};
+ return &Node{item, bottomUpTree(2*item-1, depth-1), bottomUpTree(2*item, depth-1)}
}
func (n *Node) itemCheck() int {
if n.left == nil {
return n.item
}
- return n.item + n.left.itemCheck() - n.right.itemCheck();
+ return n.item + n.left.itemCheck() - n.right.itemCheck()
}
const minDepth = 4
func main() {
- flag.Parse();
+ flag.Parse()
- maxDepth := *n;
+ maxDepth := *n
if minDepth+2 > *n {
maxDepth = minDepth + 2
}
- stretchDepth := maxDepth + 1;
+ stretchDepth := maxDepth + 1
- check := bottomUpTree(0, stretchDepth).itemCheck();
- fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check);
+ check := bottomUpTree(0, stretchDepth).itemCheck()
+ fmt.Printf("stretch tree of depth %d\t check: %d\n", stretchDepth, check)
- longLivedTree := bottomUpTree(0, maxDepth);
+ longLivedTree := bottomUpTree(0, maxDepth)
for depth := minDepth; depth <= maxDepth; depth += 2 {
- iterations := 1 << uint(maxDepth-depth+minDepth);
- check = 0;
+ iterations := 1 << uint(maxDepth-depth+minDepth)
+ check = 0
for i := 1; i <= iterations; i++ {
- check += bottomUpTree(i, depth).itemCheck();
- check += bottomUpTree(-i, depth).itemCheck();
+ check += bottomUpTree(i, depth).itemCheck()
+ check += bottomUpTree(-i, depth).itemCheck()
}
- fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check);
+ fmt.Printf("%d\t trees of depth %d\t check: %d\n", iterations*2, depth, check)
}
- fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth, longLivedTree.itemCheck());
+ fmt.Printf("long lived tree of depth %d\t check: %d\n", maxDepth, longLivedTree.itemCheck())
}
diff --git a/test/bench/chameneosredux.go b/test/bench/chameneosredux.go
index ce464dc2b..5445c4210 100644
--- a/test/bench/chameneosredux.go
+++ b/test/bench/chameneosredux.go
@@ -36,16 +36,16 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
- "strconv";
+ "flag"
+ "fmt"
+ "strconv"
)
const (
- blue = iota;
- red;
- yellow;
- ncol;
+ blue = iota
+ red
+ yellow
+ ncol
)
var complement = [...]int{
@@ -68,8 +68,8 @@ var colname = [...]string{
// information about the current state of a creature.
type info struct {
- colour int; // creature's current colour.
- name int; // creature's name.
+ colour int // creature's current colour.
+ name int // creature's name.
}
// exclusive access data-structure kept inside meetingplace.
@@ -77,21 +77,21 @@ type info struct {
// otherwise the creature's info is stored in info, and
// it is waiting to receive its mate's information on the mate channel.
type rendez struct {
- n int; // current number of encounters.
- mate chan<- info; // creature waiting when non-nil.
- info info; // info about creature waiting.
+ n int // current number of encounters.
+ mate chan<- info // creature waiting when non-nil.
+ info info // info about creature waiting.
}
// result sent by each creature at the end of processing.
type result struct {
- met int;
- same int;
+ met int
+ same int
}
var n = 600
func main() {
- flag.Parse();
+ flag.Parse()
if flag.NArg() > 0 {
n, _ = strconv.Atoi(flag.Arg(0))
}
@@ -101,72 +101,72 @@ func main() {
fmt.Printf("%s + %s -> %s\n", colname[c0], colname[c1], colname[complement[c0|c1<<2]])
}
}
- fmt.Print("\n");
+ fmt.Print("\n")
- pallmall([]int{blue, red, yellow});
- pallmall([]int{blue, red, yellow, red, yellow, blue, red, yellow, red, blue});
+ pallmall([]int{blue, red, yellow})
+ pallmall([]int{blue, red, yellow, red, yellow, blue, red, yellow, red, blue})
}
func pallmall(cols []int) {
// invariant: meetingplace always contains a value unless a creature
// is currently dealing with it (whereupon it must put it back).
- meetingplace := make(chan rendez, 1);
- meetingplace <- rendez{n: 0};
+ meetingplace := make(chan rendez, 1)
+ meetingplace <- rendez{n: 0}
- ended := make(chan result);
- msg := "";
+ ended := make(chan result)
+ msg := ""
for i, col := range cols {
- go creature(info{col, i}, meetingplace, ended);
- msg += " " + colname[col];
+ go creature(info{col, i}, meetingplace, ended)
+ msg += " " + colname[col]
}
- fmt.Println(msg);
- tot := 0;
+ fmt.Println(msg)
+ tot := 0
// wait for all results
for _ = range (cols) {
- result := <-ended;
- tot += result.met;
- fmt.Printf("%v%v\n", result.met, spell(result.same, true));
+ result := <-ended
+ tot += result.met
+ fmt.Printf("%v%v\n", result.met, spell(result.same, true))
}
- fmt.Printf("%v\n\n", spell(tot, true));
+ fmt.Printf("%v\n\n", spell(tot, true))
}
// in this function, variables ending in 0 refer to the local creature,
// variables ending in 1 to the creature we've met.
func creature(info0 info, meetingplace chan rendez, ended chan result) {
- c0 := make(chan info);
- met := 0;
- same := 0;
+ c0 := make(chan info)
+ met := 0
+ same := 0
for {
- var othername int;
+ var othername int
// get access to rendez data and decide what to do.
switch r := <-meetingplace; {
case r.n >= n:
// if no more meetings left, then send our result data and exit.
- meetingplace <- rendez{n: r.n};
- ended <- result{met, same};
- return;
+ meetingplace <- rendez{n: r.n}
+ ended <- result{met, same}
+ return
case r.mate == nil:
// no creature waiting; wait for someone to meet us,
// get their info and send our info in reply.
- meetingplace <- rendez{n: r.n, info: info0, mate: c0};
- info1 := <-c0;
- othername = info1.name;
- info0.colour = complement[info0.colour|info1.colour<<2];
+ meetingplace <- rendez{n: r.n, info: info0, mate: c0}
+ info1 := <-c0
+ othername = info1.name
+ info0.colour = complement[info0.colour|info1.colour<<2]
default:
// another creature is waiting for us with its info;
// increment meeting count,
// send them our info in reply.
- r.n++;
- meetingplace <- rendez{n: r.n, mate: nil};
- r.mate <- info0;
- othername = r.info.name;
- info0.colour = complement[info0.colour|r.info.colour<<2];
+ r.n++
+ meetingplace <- rendez{n: r.n, mate: nil}
+ r.mate <- info0
+ othername = r.info.name
+ info0.colour = complement[info0.colour|r.info.colour<<2]
}
if othername == info0.name {
same++
}
- met++;
+ met++
}
}
@@ -176,5 +176,5 @@ func spell(n int, required bool) string {
if n == 0 && !required {
return ""
}
- return spell(n/10, false) + " " + digits[n%10];
+ return spell(n/10, false) + " " + digits[n%10]
}
diff --git a/test/bench/fannkuch.go b/test/bench/fannkuch.go
index 90e6fb899..b554c77b1 100644
--- a/test/bench/fannkuch.go
+++ b/test/bench/fannkuch.go
@@ -38,8 +38,8 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
+ "flag"
+ "fmt"
)
var n = flag.Int("n", 7, "count")
@@ -49,45 +49,45 @@ func fannkuch(n int) int {
return 0
}
- n1 := n - 1;
- perm := make([]int, n);
- perm1 := make([]int, n);
- count := make([]int, n);
+ n1 := n - 1
+ perm := make([]int, n)
+ perm1 := make([]int, n)
+ count := make([]int, n)
for i := 0; i < n; i++ {
- perm1[i] = i // initial (trivial) permutation
+ perm1[i] = i // initial (trivial) permutation
}
- r := n;
- didpr := 0;
- flipsMax := 0;
+ r := n
+ didpr := 0
+ flipsMax := 0
for {
if didpr < 30 {
for i := 0; i < n; i++ {
fmt.Printf("%d", 1+perm1[i])
}
- fmt.Printf("\n");
- didpr++;
+ fmt.Printf("\n")
+ didpr++
}
for ; r != 1; r-- {
count[r-1] = r
}
if perm1[0] != 0 && perm1[n1] != n1 {
- flips := 0;
- for i := 1; i < n; i++ { // perm = perm1
+ flips := 0
+ for i := 1; i < n; i++ { // perm = perm1
perm[i] = perm1[i]
}
- k := perm1[0]; // cache perm[0] in k
- for { // k!=0 ==> k>0
+ k := perm1[0] // cache perm[0] in k
+ for { // k!=0 ==> k>0
for i, j := 1, k-1; i < j; i, j = i+1, j-1 {
perm[i], perm[j] = perm[j], perm[i]
}
- flips++;
+ flips++
// Now exchange k (caching perm[0]) and perm[k]... with care!
- j := perm[k];
- perm[k] = k;
- k = j;
+ j := perm[k]
+ perm[k] = k
+ k = j
if k == 0 {
break
}
@@ -99,12 +99,12 @@ func fannkuch(n int) int {
for ; r < n; r++ {
// rotate down perm[0..r] by one
- perm0 := perm1[0];
+ perm0 := perm1[0]
for i := 0; i < r; i++ {
perm1[i] = perm1[i+1]
}
- perm1[r] = perm0;
- count[r]--;
+ perm1[r] = perm0
+ count[r]--
if count[r] > 0 {
break
}
@@ -113,10 +113,10 @@ func fannkuch(n int) int {
return flipsMax
}
}
- return 0;
+ return 0
}
func main() {
- flag.Parse();
- fmt.Printf("Pfannkuchen(%d) = %d\n", *n, fannkuch(*n));
+ flag.Parse()
+ fmt.Printf("Pfannkuchen(%d) = %d\n", *n, fannkuch(*n))
}
diff --git a/test/bench/fasta.go b/test/bench/fasta.go
index 07d7af260..1afb1ffb8 100644
--- a/test/bench/fasta.go
+++ b/test/bench/fasta.go
@@ -38,28 +38,28 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "bufio";
- "flag";
- "os";
- "strings";
+ "bufio"
+ "flag"
+ "os"
+ "strings"
)
var out *bufio.Writer
var n = flag.Int("n", 1000, "length of result")
-const WIDTH = 60 // Fold lines after WIDTH bytes
+const WIDTH = 60 // Fold lines after WIDTH bytes
func min(a, b int) int {
if a < b {
return a
}
- return b;
+ return b
}
type AminoAcid struct {
- p float;
- c byte;
+ p float
+ c byte
}
func AccumulateProbabilities(genelist []AminoAcid) {
@@ -74,28 +74,28 @@ func AccumulateProbabilities(genelist []AminoAcid) {
// After each WIDTH characters it prints a newline.
// It assumes that WIDTH <= len(s) + 1.
func RepeatFasta(s []byte, count int) {
- pos := 0;
- s2 := make([]byte, len(s)+WIDTH);
- copy(s2, s);
- copy(s2[len(s):], s);
+ pos := 0
+ s2 := make([]byte, len(s)+WIDTH)
+ copy(s2, s)
+ copy(s2[len(s):], s)
for count > 0 {
- line := min(WIDTH, count);
- out.Write(s2[pos : pos+line]);
- out.WriteByte('\n');
- pos += line;
+ line := min(WIDTH, count)
+ out.Write(s2[pos : pos+line])
+ out.WriteByte('\n')
+ pos += line
if pos >= len(s) {
pos -= len(s)
}
- count -= line;
+ count -= line
}
}
var lastrandom uint32 = 42
const (
- IM = 139968;
- IA = 3877;
- IC = 29573;
+ IM = 139968
+ IA = 3877
+ IC = 29573
)
// Each element of genelist is a struct with a character and
@@ -107,31 +107,31 @@ const (
// This sequence is repeated count times.
// Between each WIDTH consecutive characters, the function prints a newline.
func RandomFasta(genelist []AminoAcid, count int) {
- buf := make([]byte, WIDTH+1);
+ buf := make([]byte, WIDTH+1)
for count > 0 {
- line := min(WIDTH, count);
+ line := min(WIDTH, count)
for pos := 0; pos < line; pos++ {
- lastrandom = (lastrandom*IA + IC) % IM;
+ lastrandom = (lastrandom*IA + IC) % IM
// Integer to float conversions are faster if the integer is signed.
- r := float(int32(lastrandom)) / IM;
+ r := float(int32(lastrandom)) / IM
for _, v := range genelist {
if v.p >= r {
- buf[pos] = v.c;
- break;
+ buf[pos] = v.c
+ break
}
}
}
- buf[line] = '\n';
- out.Write(buf[0 : line+1]);
- count -= line;
+ buf[line] = '\n'
+ out.Write(buf[0 : line+1])
+ count -= line
}
}
func main() {
- out = bufio.NewWriter(os.Stdout);
- defer out.Flush();
+ out = bufio.NewWriter(os.Stdout)
+ defer out.Flush()
- flag.Parse();
+ flag.Parse()
iub := []AminoAcid{
AminoAcid{0.27, 'a'},
@@ -149,17 +149,17 @@ func main() {
AminoAcid{0.02, 'V'},
AminoAcid{0.02, 'W'},
AminoAcid{0.02, 'Y'},
- };
+ }
homosapiens := []AminoAcid{
AminoAcid{0.3029549426680, 'a'},
AminoAcid{0.1979883004921, 'c'},
AminoAcid{0.1975473066391, 'g'},
AminoAcid{0.3015094502008, 't'},
- };
+ }
- AccumulateProbabilities(iub);
- AccumulateProbabilities(homosapiens);
+ AccumulateProbabilities(iub)
+ AccumulateProbabilities(homosapiens)
alu := strings.Bytes(
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
@@ -168,12 +168,12 @@ func main() {
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
- "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA");
-
- out.WriteString(">ONE Homo sapiens alu\n");
- RepeatFasta(alu, 2**n);
- out.WriteString(">TWO IUB ambiguity codes\n");
- RandomFasta(iub, 3**n);
- out.WriteString(">THREE Homo sapiens frequency\n");
- RandomFasta(homosapiens, 5**n);
+ "AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA")
+
+ out.WriteString(">ONE Homo sapiens alu\n")
+ RepeatFasta(alu, 2**n)
+ out.WriteString(">TWO IUB ambiguity codes\n")
+ RandomFasta(iub, 3**n)
+ out.WriteString(">THREE Homo sapiens frequency\n")
+ RandomFasta(homosapiens, 5**n)
}
diff --git a/test/bench/k-nucleotide.go b/test/bench/k-nucleotide.go
index 47debecb3..a5c7f79bb 100644
--- a/test/bench/k-nucleotide.go
+++ b/test/bench/k-nucleotide.go
@@ -36,71 +36,71 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "bufio";
- "bytes";
- "fmt";
- "io/ioutil";
- "os";
- "sort";
- "strings";
+ "bufio"
+ "bytes"
+ "fmt"
+ "io/ioutil"
+ "os"
+ "sort"
+ "strings"
)
var in *bufio.Reader
func count(data string, n int) map[string]int {
- counts := make(map[string]int);
- top := len(data) - n;
+ counts := make(map[string]int)
+ top := len(data) - n
for i := 0; i <= top; i++ {
- s := data[i : i+n];
+ s := data[i : i+n]
if k, ok := counts[s]; ok {
counts[s] = k + 1
} else {
counts[s] = 1
}
}
- return counts;
+ return counts
}
func countOne(data string, s string) int {
- counts := count(data, len(s));
+ counts := count(data, len(s))
if i, ok := counts[s]; ok {
return i
}
- return 0;
+ return 0
}
type kNuc struct {
- name string;
- count int;
+ name string
+ count int
}
type kNucArray []kNuc
-func (kn kNucArray) Len() int { return len(kn) }
-func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
+func (kn kNucArray) Len() int { return len(kn) }
+func (kn kNucArray) Swap(i, j int) { kn[i], kn[j] = kn[j], kn[i] }
func (kn kNucArray) Less(i, j int) bool {
if kn[i].count == kn[j].count {
- return kn[i].name > kn[j].name // sort down
+ return kn[i].name > kn[j].name // sort down
}
- return kn[i].count > kn[j].count;
+ return kn[i].count > kn[j].count
}
func sortedArray(m map[string]int) kNucArray {
- kn := make(kNucArray, len(m));
- i := 0;
+ kn := make(kNucArray, len(m))
+ i := 0
for k, v := range m {
- kn[i].name = k;
- kn[i].count = v;
- i++;
+ kn[i].name = k
+ kn[i].count = v
+ i++
}
- sort.Sort(kn);
- return kn;
+ sort.Sort(kn)
+ return kn
}
func print(m map[string]int) {
- a := sortedArray(m);
- sum := 0;
+ a := sortedArray(m)
+ sum := 0
for _, kn := range a {
sum += kn.count
}
@@ -110,40 +110,40 @@ func print(m map[string]int) {
}
func main() {
- in = bufio.NewReader(os.Stdin);
- three := strings.Bytes(">THREE ");
+ in = bufio.NewReader(os.Stdin)
+ three := strings.Bytes(">THREE ")
for {
- line, err := in.ReadSlice('\n');
+ line, err := in.ReadSlice('\n')
if err != nil {
- fmt.Fprintln(os.Stderr, "ReadLine err:", err);
- os.Exit(2);
+ fmt.Fprintln(os.Stderr, "ReadLine err:", err)
+ os.Exit(2)
}
if line[0] == '>' && bytes.Equal(line[0:len(three)], three) {
break
}
}
- data, err := ioutil.ReadAll(in);
+ data, err := ioutil.ReadAll(in)
if err != nil {
- fmt.Fprintln(os.Stderr, "ReadAll err:", err);
- os.Exit(2);
+ fmt.Fprintln(os.Stderr, "ReadAll err:", err)
+ os.Exit(2)
}
// delete the newlines and convert to upper case
- j := 0;
+ j := 0
for i := 0; i < len(data); i++ {
if data[i] != '\n' {
- data[j] = data[i] &^ ' '; // upper case
- j++;
+ data[j] = data[i] &^ ' ' // upper case
+ j++
}
}
- str := string(data[0:j]);
+ str := string(data[0:j])
- print(count(str, 1));
- fmt.Print("\n");
+ print(count(str, 1))
+ fmt.Print("\n")
- print(count(str, 2));
- fmt.Print("\n");
+ print(count(str, 2))
+ fmt.Print("\n")
- interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"};
+ interests := []string{"GGT", "GGTA", "GGTATT", "GGTATTTTAATT", "GGTATTTTAATTTATAGT"}
for _, s := range interests {
fmt.Printf("%d %s\n", countOne(str, s), s)
}
diff --git a/test/bench/mandelbrot.go b/test/bench/mandelbrot.go
index 6c1b7d4e5..1f9fbfd3d 100644
--- a/test/bench/mandelbrot.go
+++ b/test/bench/mandelbrot.go
@@ -37,58 +37,58 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "bufio";
- "flag";
- "fmt";
- "os";
+ "bufio"
+ "flag"
+ "fmt"
+ "os"
)
var n = flag.Int("n", 200, "size")
func main() {
- flag.Parse();
- out := bufio.NewWriter(os.Stdout);
- defer out.Flush();
+ flag.Parse()
+ out := bufio.NewWriter(os.Stdout)
+ defer out.Flush()
- w := *n;
- h := *n;
- bit_num := 0;
- byte_acc := byte(0);
- const Iter = 50;
- const Zero float64 = 0;
- const Limit = 2.0;
+ w := *n
+ h := *n
+ bit_num := 0
+ byte_acc := byte(0)
+ const Iter = 50
+ const Zero float64 = 0
+ const Limit = 2.0
- fmt.Fprintf(out, "P4\n%d %d\n", w, h);
+ fmt.Fprintf(out, "P4\n%d %d\n", w, h)
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
- Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero;
- Cr := (2*float64(x)/float64(w) - 1.5);
- Ci := (2*float64(y)/float64(h) - 1.0);
+ Zr, Zi, Tr, Ti := Zero, Zero, Zero, Zero
+ Cr := (2*float64(x)/float64(w) - 1.5)
+ Ci := (2*float64(y)/float64(h) - 1.0)
for i := 0; i < Iter && (Tr+Ti <= Limit*Limit); i++ {
- Zi = 2*Zr*Zi + Ci;
- Zr = Tr - Ti + Cr;
- Tr = Zr * Zr;
- Ti = Zi * Zi;
+ Zi = 2*Zr*Zi + Ci
+ Zr = Tr - Ti + Cr
+ Tr = Zr * Zr
+ Ti = Zi * Zi
}
- byte_acc <<= 1;
+ byte_acc <<= 1
if Tr+Ti <= Limit*Limit {
byte_acc |= 0x01
}
- bit_num++;
+ bit_num++
if bit_num == 8 {
- out.WriteByte(byte_acc);
- byte_acc = 0;
- bit_num = 0;
+ out.WriteByte(byte_acc)
+ byte_acc = 0
+ bit_num = 0
} else if x == w-1 {
- byte_acc <<= uint(8 - w%8);
- out.WriteByte(byte_acc);
- byte_acc = 0;
- bit_num = 0;
+ byte_acc <<= uint(8 - w%8)
+ out.WriteByte(byte_acc)
+ byte_acc = 0
+ bit_num = 0
}
}
}
diff --git a/test/bench/meteor-contest.go b/test/bench/meteor-contest.go
index a93938999..163aaa7c4 100644
--- a/test/bench/meteor-contest.go
+++ b/test/bench/meteor-contest.go
@@ -37,8 +37,8 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
+ "flag"
+ "fmt"
)
var max_solutions = flag.Int("n", 2100, "maximum number of solutions")
@@ -48,7 +48,7 @@ func boolInt(b bool) int8 {
if b {
return 1
}
- return 0;
+ return 0
}
/* The board is a 50 cell hexagonal pattern. For . . . . .
@@ -87,19 +87,19 @@ var board uint64 = 0xFFFC000000000000
*/
const (
- E = iota;
- ESE;
- SE;
- S;
- SW;
- WSW;
- W;
- WNW;
- NW;
- N;
- NE;
- ENE;
- PIVOT;
+ E = iota
+ ESE
+ SE
+ S
+ SW
+ WSW
+ W
+ WNW
+ NW
+ N
+ NE
+ ENE
+ PIVOT
)
var piece_def = [10][4]int8{
@@ -127,16 +127,16 @@ var piece_def = [10][4]int8{
* location to reduce the burden on the solve function.
*/
var (
- pieces [10][50][12]uint64;
- piece_counts [10][50]int;
- next_cell [10][50][12]int8;
+ pieces [10][50][12]uint64
+ piece_counts [10][50]int
+ next_cell [10][50][12]int8
)
/* Returns the direction rotated 60 degrees clockwise */
-func rotate(dir int8) int8 { return (dir + 2) % PIVOT }
+func rotate(dir int8) int8 { return (dir + 2) % PIVOT }
/* Returns the direction flipped on the horizontal axis */
-func flip(dir int8) int8 { return (PIVOT - dir) % PIVOT }
+func flip(dir int8) int8 { return (PIVOT - dir) % PIVOT }
/* Returns the new cell index from the specified cell in the
@@ -203,7 +203,7 @@ func shift(cell, dir int8) int8 {
return cell - 4
}
}
- return cell;
+ return cell
}
/* Returns wether the specified cell and direction will land outside
@@ -215,8 +215,8 @@ func out_of_bounds(cell, dir int8) bool {
case E:
return cell%5 == 4
case ESE:
- i := cell % 10;
- return i == 4 || i == 8 || i == 9 || cell >= 45;
+ i := cell % 10
+ return i == 4 || i == 8 || i == 9 || cell >= 45
case SE:
return cell%10 == 9 || cell >= 45
case S:
@@ -224,13 +224,13 @@ func out_of_bounds(cell, dir int8) bool {
case SW:
return cell%10 == 0 || cell >= 45
case WSW:
- i := cell % 10;
- return i == 0 || i == 1 || i == 5 || cell >= 45;
+ i := cell % 10
+ return i == 0 || i == 1 || i == 5 || cell >= 45
case W:
return cell%5 == 0
case WNW:
- i := cell % 10;
- return i == 0 || i == 1 || i == 5 || cell < 5;
+ i := cell % 10
+ return i == 0 || i == 1 || i == 5 || cell < 5
case NW:
return cell%10 == 0 || cell < 5
case N:
@@ -238,10 +238,10 @@ func out_of_bounds(cell, dir int8) bool {
case NE:
return cell%10 == 9 || cell < 5
case ENE:
- i := cell % 10;
- return i == 4 || i == 8 || i == 9 || cell < 5;
+ i := cell % 10
+ return i == 4 || i == 8 || i == 9 || cell < 5
}
- return false;
+ return false
}
/* Rotate a piece 60 degrees clockwise */
@@ -260,7 +260,7 @@ func flip_piece(piece int) {
/* Convenience function to quickly calculate all of the indices for a piece */
func calc_cell_indices(cell []int8, piece int, index int8) {
- cell[0] = index;
+ cell[0] = index
for i := 1; i < 5; i++ {
cell[i] = shift(cell[i-1], piece_def[piece][i-1])
}
@@ -279,13 +279,13 @@ func cells_fit_on_board(cell []int8, piece int) bool {
* the piece in the solve function.
*/
func minimum_of_cells(cell []int8) int8 {
- minimum := cell[0];
+ minimum := cell[0]
for i := 1; i < 5; i++ {
if cell[i] < minimum {
minimum = cell[i]
}
}
- return minimum;
+ return minimum
}
/* Calculate the lowest possible open cell if the piece is placed on the board.
@@ -293,33 +293,33 @@ func minimum_of_cells(cell []int8) int8 {
* solve function.
*/
func first_empty_cell(cell []int8, minimum int8) int8 {
- first_empty := minimum;
+ first_empty := minimum
for first_empty == cell[0] || first_empty == cell[1] ||
first_empty == cell[2] || first_empty == cell[3] ||
first_empty == cell[4] {
first_empty++
}
- return first_empty;
+ return first_empty
}
/* Generate the unsigned long long int that will later be anded with the
* board to determine if it fits.
*/
func bitmask_from_cells(cell []int8) uint64 {
- var piece_mask uint64;
+ var piece_mask uint64
for i := 0; i < 5; i++ {
piece_mask |= 1 << uint(cell[i])
}
- return piece_mask;
+ return piece_mask
}
/* Record the piece and other important information in arrays that will
* later be used by the solve function.
*/
func record_piece(piece int, minimum int8, first_empty int8, piece_mask uint64) {
- pieces[piece][minimum][piece_counts[piece][minimum]] = piece_mask;
- next_cell[piece][minimum][piece_counts[piece][minimum]] = first_empty;
- piece_counts[piece][minimum]++;
+ pieces[piece][minimum][piece_counts[piece][minimum]] = piece_mask
+ next_cell[piece][minimum][piece_counts[piece][minimum]] = first_empty
+ piece_counts[piece][minimum]++
}
@@ -330,7 +330,7 @@ func fill_contiguous_space(board []int8, index int8) {
if board[index] == 1 {
return
}
- board[index] = 1;
+ board[index] = 1
if !out_of_bounds(index, E) {
fill_contiguous_space(board, shift(index, E))
}
@@ -359,17 +359,17 @@ func fill_contiguous_space(board []int8, index int8) {
* can split the board in half where both halves are viable.
*/
func has_island(cell []int8, piece int) bool {
- temp_board := make([]int8, 50);
- var i int;
+ temp_board := make([]int8, 50)
+ var i int
for i = 0; i < 5; i++ {
temp_board[cell[i]] = 1
}
- i = 49;
+ i = 49
for temp_board[i] == 1 {
i--
}
- fill_contiguous_space(temp_board, int8(i));
- c := 0;
+ fill_contiguous_space(temp_board, int8(i))
+ c := 0
for i = 0; i < 50; i++ {
if temp_board[i] == 0 {
c++
@@ -379,7 +379,7 @@ func has_island(cell []int8, piece int) bool {
(c%5 == 0 && piece == 0) {
return false
}
- return true;
+ return true
}
@@ -391,18 +391,18 @@ func has_island(cell []int8, piece int) bool {
* me the best time ;)
*/
func calc_six_rotations(piece, index int) {
- cell := make([]int8, 5);
+ cell := make([]int8, 5)
for rotation := 0; rotation < 6; rotation++ {
if piece != 3 || rotation < 3 {
- calc_cell_indices(cell, piece, int8(index));
+ calc_cell_indices(cell, piece, int8(index))
if cells_fit_on_board(cell, piece) && !has_island(cell, piece) {
- minimum := minimum_of_cells(cell);
- first_empty := first_empty_cell(cell, minimum);
- piece_mask := bitmask_from_cells(cell);
- record_piece(piece, minimum, first_empty, piece_mask);
+ minimum := minimum_of_cells(cell)
+ first_empty := first_empty_cell(cell, minimum)
+ piece_mask := bitmask_from_cells(cell)
+ record_piece(piece, minimum, first_empty, piece_mask)
}
}
- rotate_piece(piece);
+ rotate_piece(piece)
}
}
@@ -410,9 +410,9 @@ func calc_six_rotations(piece, index int) {
func calc_pieces() {
for piece := 0; piece < 10; piece++ {
for index := 0; index < 50; index++ {
- calc_six_rotations(piece, index);
- flip_piece(piece);
- calc_six_rotations(piece, index);
+ calc_six_rotations(piece, index)
+ flip_piece(piece)
+ calc_six_rotations(piece, index)
}
}
}
@@ -424,41 +424,41 @@ func calc_pieces() {
* board in the solve function.
*/
const (
- ROW_MASK = 0x1F;
- TRIPLE_MASK = 0x7FFF;
+ ROW_MASK = 0x1F
+ TRIPLE_MASK = 0x7FFF
)
var (
- all_rows = [32]int8{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
+ all_rows = [32]int8{0, 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,
- };
- bad_even_rows [32][32]int8;
- bad_odd_rows [32][32]int8;
- bad_even_triple [32768]int8;
- bad_odd_triple [32768]int8;
+ }
+ bad_even_rows [32][32]int8
+ bad_odd_rows [32][32]int8
+ bad_even_triple [32768]int8
+ bad_odd_triple [32768]int8
)
func rows_bad(row1, row2 int8, even bool) int8 {
/* even is referring to row1 */
- var row2_shift int8;
+ var row2_shift int8
/* Test for blockages at same index and shifted index */
if even {
row2_shift = ((row2 << 1) & ROW_MASK) | 0x01
} else {
row2_shift = (row2 >> 1) | 0x10
}
- block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift);
+ block := ((row1 ^ row2) & row2) & ((row1 ^ row2_shift) & row2_shift)
/* Test for groups of 0's */
- in_zeroes := false;
- group_okay := false;
+ in_zeroes := false
+ group_okay := false
for i := uint8(0); i < 5; i++ {
if row1&(1<<i) != 0 {
if in_zeroes {
if !group_okay {
return 1
}
- in_zeroes = false;
- group_okay = false;
+ in_zeroes = false
+ group_okay = false
}
} else {
if !in_zeroes {
@@ -472,7 +472,7 @@ func rows_bad(row1, row2 int8, even bool) int8 {
if in_zeroes {
return boolInt(!group_okay)
}
- return 0;
+ return 0
}
/* Check for cases where three rows checked sequentially cause a false
@@ -497,29 +497,29 @@ func triple_is_okay(row1, row2, row3 int, even bool) bool {
* row3: ????? ?????
*/
return ((row1 == 0x13) && (row2 == 0x11)) ||
- ((row1 == 0x15) && (row2 == 0x11));
+ ((row1 == 0x15) && (row2 == 0x11))
}
func calc_rows() {
for row1 := int8(0); row1 < 32; row1++ {
for row2 := int8(0); row2 < 32; row2++ {
- bad_even_rows[row1][row2] = rows_bad(row1, row2, true);
- bad_odd_rows[row1][row2] = rows_bad(row1, row2, false);
+ bad_even_rows[row1][row2] = rows_bad(row1, row2, true)
+ bad_odd_rows[row1][row2] = rows_bad(row1, row2, false)
}
}
for row1 := 0; row1 < 32; row1++ {
for row2 := 0; row2 < 32; row2++ {
for row3 := 0; row3 < 32; row3++ {
- result1 := bad_even_rows[row1][row2];
- result2 := bad_odd_rows[row2][row3];
+ result1 := bad_even_rows[row1][row2]
+ result2 := bad_odd_rows[row2][row3]
if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, true) {
bad_even_triple[row1+(row2*32)+(row3*1024)] = 0
} else {
bad_even_triple[row1+(row2*32)+(row3*1024)] = boolInt(result1 != 0 || result2 != 0)
}
- result1 = bad_odd_rows[row1][row2];
- result2 = bad_even_rows[row2][row3];
+ result1 = bad_odd_rows[row1][row2]
+ result2 = bad_even_rows[row2][row3]
if result1 == 0 && result2 != 0 && triple_is_okay(row1, row2, row3, false) {
bad_odd_triple[row1+(row2*32)+(row3*1024)] = 0
} else {
@@ -538,11 +538,11 @@ func boardHasIslands(cell int8) int8 {
if cell >= 40 {
return 0
}
- current_triple := (board >> uint((cell/5)*5)) & TRIPLE_MASK;
+ current_triple := (board >> uint((cell/5)*5)) & TRIPLE_MASK
if (cell/5)%2 != 0 {
return bad_odd_triple[current_triple]
}
- return bad_even_triple[current_triple];
+ return bad_even_triple[current_triple]
}
@@ -553,26 +553,26 @@ func boardHasIslands(cell int8) int8 {
* array if a solution is found.
*/
var (
- avail uint16 = 0x03FF;
- sol_nums [10]int8;
- sol_masks [10]uint64;
- solutions [2100][50]int8;
- solution_count = 0;
+ avail uint16 = 0x03FF
+ sol_nums [10]int8
+ sol_masks [10]uint64
+ solutions [2100][50]int8
+ solution_count = 0
)
func record_solution() {
for sol_no := 0; sol_no < 10; sol_no++ {
- sol_mask := sol_masks[sol_no];
+ sol_mask := sol_masks[sol_no]
for index := 0; index < 50; index++ {
if sol_mask&1 == 1 {
- solutions[solution_count][index] = sol_nums[sol_no];
+ solutions[solution_count][index] = sol_nums[sol_no]
/* Board rotated 180 degrees is a solution too! */
- solutions[solution_count+1][49-index] = sol_nums[sol_no];
+ solutions[solution_count+1][49-index] = sol_nums[sol_no]
}
- sol_mask = sol_mask >> 1;
+ sol_mask = sol_mask >> 1
}
}
- solution_count += 2;
+ solution_count += 2
}
func solve(depth, cell int8) {
@@ -585,31 +585,31 @@ func solve(depth, cell int8) {
}
for piece := int8(0); piece < 10; piece++ {
- var piece_no_mask uint16 = 1 << uint(piece);
+ var piece_no_mask uint16 = 1 << uint(piece)
if avail&piece_no_mask == 0 {
continue
}
- avail ^= piece_no_mask;
- max_rots := piece_counts[piece][cell];
- piece_mask := pieces[piece][cell];
+ avail ^= piece_no_mask
+ max_rots := piece_counts[piece][cell]
+ piece_mask := pieces[piece][cell]
for rotation := 0; rotation < max_rots; rotation++ {
if board&piece_mask[rotation] == 0 {
- sol_nums[depth] = piece;
- sol_masks[depth] = piece_mask[rotation];
+ sol_nums[depth] = piece
+ sol_masks[depth] = piece_mask[rotation]
if depth == 9 {
/* Solution found!!!!!11!!ONE! */
- record_solution();
- avail ^= piece_no_mask;
- return;
+ record_solution()
+ avail ^= piece_no_mask
+ return
}
- board |= piece_mask[rotation];
+ board |= piece_mask[rotation]
if boardHasIslands(next_cell[piece][cell][rotation]) == 0 {
solve(depth+1, next_cell[piece][cell][rotation])
}
- board ^= piece_mask[rotation];
+ board ^= piece_mask[rotation]
}
}
- avail ^= piece_no_mask;
+ avail ^= piece_no_mask
}
}
@@ -620,46 +620,46 @@ func pretty(b *[50]int8) {
b[i+2]+'0', b[i+3]+'0', b[i+4]+'0', b[i+5]+'0', b[i+6]+'0',
b[i+7]+'0', b[i+8]+'0', b[i+9]+'0')
}
- fmt.Printf("\n");
+ fmt.Printf("\n")
}
/* Find smallest and largest solutions */
func smallest_largest() (smallest, largest *[50]int8) {
- smallest = &solutions[0];
- largest = &solutions[0];
+ smallest = &solutions[0]
+ largest = &solutions[0]
for i := 1; i < solution_count; i++ {
- candidate := &solutions[i];
+ candidate := &solutions[i]
for j, s := range *smallest {
- c := candidate[j];
+ c := candidate[j]
if c == s {
continue
}
if c < s {
smallest = candidate
}
- break;
+ break
}
for j, s := range *largest {
- c := candidate[j];
+ c := candidate[j]
if c == s {
continue
}
if c > s {
largest = candidate
}
- break;
+ break
}
}
- return;
+ return
}
func main() {
- flag.Parse();
- calc_pieces();
- calc_rows();
- solve(0, 0);
- fmt.Printf("%d solutions found\n\n", solution_count);
- smallest, largest := smallest_largest();
- pretty(smallest);
- pretty(largest);
+ flag.Parse()
+ calc_pieces()
+ calc_rows()
+ solve(0, 0)
+ fmt.Printf("%d solutions found\n\n", solution_count)
+ smallest, largest := smallest_largest()
+ pretty(smallest)
+ pretty(largest)
}
diff --git a/test/bench/nbody.go b/test/bench/nbody.go
index 05831bba2..e9f4517e8 100644
--- a/test/bench/nbody.go
+++ b/test/bench/nbody.go
@@ -37,94 +37,94 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
- "math";
+ "flag"
+ "fmt"
+ "math"
)
var n = flag.Int("n", 1000, "number of iterations")
type Body struct {
- x, y, z, vx, vy, vz, mass float64;
+ x, y, z, vx, vy, vz, mass float64
}
const (
- solarMass = 4 * math.Pi * math.Pi;
- daysPerYear = 365.24;
+ solarMass = 4 * math.Pi * math.Pi
+ daysPerYear = 365.24
)
func (b *Body) offsetMomentum(px, py, pz float64) {
- b.vx = -px / solarMass;
- b.vy = -py / solarMass;
- b.vz = -pz / solarMass;
+ b.vx = -px / solarMass
+ b.vy = -py / solarMass
+ b.vz = -pz / solarMass
}
type System []*Body
func NewSystem(body []Body) System {
- n := make(System, len(body));
+ n := make(System, len(body))
for i := 0; i < len(body); i++ {
- n[i] = new(Body); // copy to avoid overwriting the inputs
- *n[i] = body[i];
+ n[i] = new(Body) // copy to avoid overwriting the inputs
+ *n[i] = body[i]
}
- var px, py, pz float64;
+ var px, py, pz float64
for _, body := range n {
- px += body.vx * body.mass;
- py += body.vy * body.mass;
- pz += body.vz * body.mass;
+ px += body.vx * body.mass
+ py += body.vy * body.mass
+ pz += body.vz * body.mass
}
- n[0].offsetMomentum(px, py, pz);
- return n;
+ n[0].offsetMomentum(px, py, pz)
+ return n
}
func (sys System) energy() float64 {
- var e float64;
+ var e float64
for i, body := range sys {
e += 0.5 * body.mass *
- (body.vx*body.vx + body.vy*body.vy + body.vz*body.vz);
+ (body.vx*body.vx + body.vy*body.vy + body.vz*body.vz)
for j := i + 1; j < len(sys); j++ {
- body2 := sys[j];
- dx := body.x - body2.x;
- dy := body.y - body2.y;
- dz := body.z - body2.z;
- distance := math.Sqrt(dx*dx + dy*dy + dz*dz);
- e -= (body.mass * body2.mass) / distance;
+ body2 := sys[j]
+ dx := body.x - body2.x
+ dy := body.y - body2.y
+ dz := body.z - body2.z
+ distance := math.Sqrt(dx*dx + dy*dy + dz*dz)
+ e -= (body.mass * body2.mass) / distance
}
}
- return e;
+ return e
}
func (sys System) advance(dt float64) {
for i, body := range sys {
for j := i + 1; j < len(sys); j++ {
- body2 := sys[j];
- dx := body.x - body2.x;
- dy := body.y - body2.y;
- dz := body.z - body2.z;
-
- dSquared := dx*dx + dy*dy + dz*dz;
- distance := math.Sqrt(dSquared);
- mag := dt / (dSquared * distance);
-
- body.vx -= dx * body2.mass * mag;
- body.vy -= dy * body2.mass * mag;
- body.vz -= dz * body2.mass * mag;
-
- body2.vx += dx * body.mass * mag;
- body2.vy += dy * body.mass * mag;
- body2.vz += dz * body.mass * mag;
+ body2 := sys[j]
+ dx := body.x - body2.x
+ dy := body.y - body2.y
+ dz := body.z - body2.z
+
+ dSquared := dx*dx + dy*dy + dz*dz
+ distance := math.Sqrt(dSquared)
+ mag := dt / (dSquared * distance)
+
+ body.vx -= dx * body2.mass * mag
+ body.vy -= dy * body2.mass * mag
+ body.vz -= dz * body2.mass * mag
+
+ body2.vx += dx * body.mass * mag
+ body2.vy += dy * body.mass * mag
+ body2.vz += dz * body.mass * mag
}
}
for _, body := range sys {
- body.x += dt * body.vx;
- body.y += dt * body.vy;
- body.z += dt * body.vz;
+ body.x += dt * body.vx
+ body.y += dt * body.vy
+ body.z += dt * body.vz
}
}
var (
- jupiter = Body{
+ jupiter = Body{
x: 4.84143144246472090e+00,
y: -1.16032004402742839e+00,
z: -1.03622044471123109e-01,
@@ -132,8 +132,8 @@ var (
vy: 7.69901118419740425e-03 * daysPerYear,
vz: -6.90460016972063023e-05 * daysPerYear,
mass: 9.54791938424326609e-04 * solarMass,
- };
- saturn = Body{
+ }
+ saturn = Body{
x: 8.34336671824457987e+00,
y: 4.12479856412430479e+00,
z: -4.03523417114321381e-01,
@@ -141,8 +141,8 @@ var (
vy: 4.99852801234917238e-03 * daysPerYear,
vz: 2.30417297573763929e-05 * daysPerYear,
mass: 2.85885980666130812e-04 * solarMass,
- };
- uranus = Body{
+ }
+ uranus = Body{
x: 1.28943695621391310e+01,
y: -1.51111514016986312e+01,
z: -2.23307578892655734e-01,
@@ -150,8 +150,8 @@ var (
vy: 2.37847173959480950e-03 * daysPerYear,
vz: -2.96589568540237556e-05 * daysPerYear,
mass: 4.36624404335156298e-05 * solarMass,
- };
- neptune = Body{
+ }
+ neptune = Body{
x: 1.53796971148509165e+01,
y: -2.59193146099879641e+01,
z: 1.79258772950371181e-01,
@@ -159,19 +159,19 @@ var (
vy: 1.62824170038242295e-03 * daysPerYear,
vz: -9.51592254519715870e-05 * daysPerYear,
mass: 5.15138902046611451e-05 * solarMass,
- };
- sun = Body{
+ }
+ sun = Body{
mass: solarMass,
- };
+ }
)
func main() {
- flag.Parse();
+ flag.Parse()
- system := NewSystem([]Body{sun, jupiter, saturn, uranus, neptune});
- fmt.Printf("%.9f\n", system.energy());
+ system := NewSystem([]Body{sun, jupiter, saturn, uranus, neptune})
+ fmt.Printf("%.9f\n", system.energy())
for i := 0; i < *n; i++ {
system.advance(0.01)
}
- fmt.Printf("%.9f\n", system.energy());
+ fmt.Printf("%.9f\n", system.energy())
}
diff --git a/test/bench/pidigits.go b/test/bench/pidigits.go
index d0dfc966b..430c11828 100644
--- a/test/bench/pidigits.go
+++ b/test/bench/pidigits.go
@@ -38,20 +38,20 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "bignum";
- "flag";
- "fmt";
+ "bignum"
+ "flag"
+ "fmt"
)
var n = flag.Int("n", 27, "number of digits")
var silent = flag.Bool("s", false, "don't print result")
var (
- tmp1 *bignum.Integer;
- tmp2 *bignum.Integer;
- numer = bignum.Int(1);
- accum = bignum.Int(0);
- denom = bignum.Int(1);
+ tmp1 *bignum.Integer
+ tmp2 *bignum.Integer
+ numer = bignum.Int(1)
+ accum = bignum.Int(0)
+ denom = bignum.Int(1)
)
func extract_digit() int64 {
@@ -60,36 +60,36 @@ func extract_digit() int64 {
}
// Compute (numer * 3 + accum) / denom
- tmp1 = numer.Shl(1);
- bignum.Iadd(tmp1, tmp1, numer);
- bignum.Iadd(tmp1, tmp1, accum);
- tmp1, tmp2 := tmp1.QuoRem(denom);
+ tmp1 = numer.Shl(1)
+ bignum.Iadd(tmp1, tmp1, numer)
+ bignum.Iadd(tmp1, tmp1, accum)
+ tmp1, tmp2 := tmp1.QuoRem(denom)
// Now, if (numer * 4 + accum) % denom...
- bignum.Iadd(tmp2, tmp2, numer);
+ bignum.Iadd(tmp2, tmp2, numer)
// ... is normalized, then the two divisions have the same result.
if tmp2.Cmp(denom) >= 0 {
return -1
}
- return tmp1.Value();
+ return tmp1.Value()
}
func next_term(k int64) {
- y2 := k*2 + 1;
+ y2 := k*2 + 1
- tmp1 = numer.Shl(1);
- bignum.Iadd(accum, accum, tmp1);
- bignum.Iscale(accum, y2);
- bignum.Iscale(numer, k);
- bignum.Iscale(denom, y2);
+ tmp1 = numer.Shl(1)
+ bignum.Iadd(accum, accum, tmp1)
+ bignum.Iscale(accum, y2)
+ bignum.Iscale(numer, k)
+ bignum.Iscale(denom, y2)
}
func eliminate_digit(d int64) {
- bignum.Isub(accum, accum, denom.Mul1(d));
- bignum.Iscale(accum, 10);
- bignum.Iscale(numer, 10);
+ bignum.Isub(accum, accum, denom.Mul1(d))
+ bignum.Iscale(accum, 10)
+ bignum.Iscale(numer, 10)
}
func printf(s string, arg ...) {
@@ -99,28 +99,28 @@ func printf(s string, arg ...) {
}
func main() {
- flag.Parse();
+ flag.Parse()
- var m int; // 0 <= m < 10
+ var m int // 0 <= m < 10
for i, k := 0, int64(0); ; {
- d := int64(-1);
+ d := int64(-1)
for d < 0 {
- k++;
- next_term(k);
- d = extract_digit();
+ k++
+ next_term(k)
+ d = extract_digit()
}
- printf("%c", d+'0');
+ printf("%c", d+'0')
- i++;
- m = i % 10;
+ i++
+ m = i % 10
if m == 0 {
printf("\t:%d\n", i)
}
if i >= *n {
break
}
- eliminate_digit(d);
+ eliminate_digit(d)
}
if m > 0 {
diff --git a/test/bench/regex-dna.go b/test/bench/regex-dna.go
index 2e1ab8edb..9d56830b3 100644
--- a/test/bench/regex-dna.go
+++ b/test/bench/regex-dna.go
@@ -36,11 +36,11 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "fmt";
- "io/ioutil";
- "os";
- "regexp";
- "strings";
+ "fmt"
+ "io/ioutil"
+ "os"
+ "regexp"
+ "strings"
)
var variants = []string{
@@ -56,7 +56,7 @@ var variants = []string{
}
type Subst struct {
- pat, repl string;
+ pat, repl string
}
var substs = []Subst{
@@ -74,34 +74,34 @@ var substs = []Subst{
}
func countMatches(pat string, bytes []byte) int {
- re := regexp.MustCompile(pat);
- n := 0;
+ re := regexp.MustCompile(pat)
+ n := 0
for {
- e := re.Execute(bytes);
+ e := re.Execute(bytes)
if len(e) == 0 {
break
}
- n++;
- bytes = bytes[e[1]:];
+ n++
+ bytes = bytes[e[1]:]
}
- return n;
+ return n
}
func main() {
- bytes, err := ioutil.ReadFile("/dev/stdin");
+ bytes, err := ioutil.ReadFile("/dev/stdin")
if err != nil {
- fmt.Fprintf(os.Stderr, "can't read input: %s\n", err);
- os.Exit(2);
+ fmt.Fprintf(os.Stderr, "can't read input: %s\n", err)
+ os.Exit(2)
}
- ilen := len(bytes);
+ ilen := len(bytes)
// Delete the comment lines and newlines
- bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{});
- clen := len(bytes);
+ bytes = regexp.MustCompile("(>[^\n]+)?\n").ReplaceAll(bytes, []byte{})
+ clen := len(bytes)
for _, s := range variants {
fmt.Printf("%s %d\n", s, countMatches(s, bytes))
}
for _, sub := range substs {
bytes = regexp.MustCompile(sub.pat).ReplaceAll(bytes, strings.Bytes(sub.repl))
}
- fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes));
+ fmt.Printf("\n%d\n%d\n%d\n", ilen, clen, len(bytes))
}
diff --git a/test/bench/reverse-complement.go b/test/bench/reverse-complement.go
index a685e43b5..baa30ffcc 100644
--- a/test/bench/reverse-complement.go
+++ b/test/bench/reverse-complement.go
@@ -36,8 +36,8 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "bufio";
- "os";
+ "bufio"
+ "os"
)
const lineSize = 60
@@ -62,44 +62,44 @@ var complement = [256]uint8{
}
func main() {
- in := bufio.NewReader(os.Stdin);
- buf := make([]byte, 1024*1024);
- line, err := in.ReadSlice('\n');
+ in := bufio.NewReader(os.Stdin)
+ buf := make([]byte, 1024*1024)
+ line, err := in.ReadSlice('\n')
for err == nil {
- os.Stdout.Write(line);
+ os.Stdout.Write(line)
// Accumulate reversed complement in buf[w:]
- nchar := 0;
- w := len(buf);
+ nchar := 0
+ w := len(buf)
for {
- line, err = in.ReadSlice('\n');
+ line, err = in.ReadSlice('\n')
if err != nil || line[0] == '>' {
break
}
- line = line[0 : len(line)-1];
- nchar += len(line);
+ line = line[0 : len(line)-1]
+ nchar += len(line)
if len(line)+nchar/60+128 >= w {
- nbuf := make([]byte, len(buf)*5);
- copy(nbuf[len(nbuf)-len(buf):], buf);
- w += len(nbuf) - len(buf);
- buf = nbuf;
+ nbuf := make([]byte, len(buf)*5)
+ copy(nbuf[len(nbuf)-len(buf):], buf)
+ w += len(nbuf) - len(buf)
+ buf = nbuf
}
// This loop is the bottleneck.
for _, c := range line {
- w--;
- buf[w] = complement[c];
+ w--
+ buf[w] = complement[c]
}
}
// Copy down to beginning of buffer, inserting newlines.
// The loop left room for the newlines and 128 bytes of padding.
- i := 0;
+ i := 0
for j := w; j < len(buf); j += 60 {
- n := copy(buf[i:i+60], buf[j:]);
- buf[i+n] = '\n';
- i += n + 1;
+ n := copy(buf[i:i+60], buf[j:])
+ buf[i+n] = '\n'
+ i += n + 1
}
- os.Stdout.Write(buf[0:i]);
+ os.Stdout.Write(buf[0:i])
}
}
diff --git a/test/bench/spectral-norm-parallel.go b/test/bench/spectral-norm-parallel.go
index 47882c69d..2706f39ec 100644
--- a/test/bench/spectral-norm-parallel.go
+++ b/test/bench/spectral-norm-parallel.go
@@ -37,37 +37,37 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
- "math";
- "runtime";
+ "flag"
+ "fmt"
+ "math"
+ "runtime"
)
var n = flag.Int("n", 2000, "count")
var nCPU = flag.Int("ncpu", 4, "number of cpus")
-func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
+func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
type Vec []float64
func (v Vec) Times(i, n int, u Vec, c chan int) {
for ; i < n; i++ {
- v[i] = 0;
+ v[i] = 0
for j := 0; j < len(u); j++ {
v[i] += evalA(i, j) * u[j]
}
}
- c <- 1;
+ c <- 1
}
func (v Vec) TimesTransp(i, n int, u Vec, c chan int) {
for ; i < n; i++ {
- v[i] = 0;
+ v[i] = 0
for j := 0; j < len(u); j++ {
v[i] += evalA(j, i) * u[j]
}
}
- c <- 1;
+ c <- 1
}
func wait(c chan int) {
@@ -77,35 +77,35 @@ func wait(c chan int) {
}
func (v Vec) ATimesTransp(u Vec) {
- x := make(Vec, len(u));
- c := make(chan int, *nCPU);
+ x := make(Vec, len(u))
+ c := make(chan int, *nCPU)
for i := 0; i < *nCPU; i++ {
go x.Times(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, u, c)
}
- wait(c);
+ wait(c)
for i := 0; i < *nCPU; i++ {
go v.TimesTransp(i*len(v) / *nCPU, (i+1)*len(v) / *nCPU, x, c)
}
- wait(c);
+ wait(c)
}
func main() {
- flag.Parse();
- runtime.GOMAXPROCS(*nCPU);
- N := *n;
- u := make(Vec, N);
+ flag.Parse()
+ runtime.GOMAXPROCS(*nCPU)
+ N := *n
+ u := make(Vec, N)
for i := 0; i < N; i++ {
u[i] = 1
}
- v := make(Vec, N);
+ v := make(Vec, N)
for i := 0; i < 10; i++ {
- v.ATimesTransp(u);
- u.ATimesTransp(v);
+ v.ATimesTransp(u)
+ u.ATimesTransp(v)
}
- var vBv, vv float64;
+ var vBv, vv float64
for i := 0; i < N; i++ {
- vBv += u[i] * v[i];
- vv += v[i] * v[i];
+ vBv += u[i] * v[i]
+ vv += v[i] * v[i]
}
- fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
+ fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv))
}
diff --git a/test/bench/spectral-norm.go b/test/bench/spectral-norm.go
index e79bc282e..6667f3e04 100644
--- a/test/bench/spectral-norm.go
+++ b/test/bench/spectral-norm.go
@@ -37,20 +37,20 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
- "math";
+ "flag"
+ "fmt"
+ "math"
)
var n = flag.Int("n", 2000, "count")
-func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
+func evalA(i, j int) float64 { return 1 / float64(((i+j)*(i+j+1)/2 + i + 1)) }
type Vec []float64
func (v Vec) Times(u Vec) {
for i := 0; i < len(v); i++ {
- v[i] = 0;
+ v[i] = 0
for j := 0; j < len(u); j++ {
v[i] += evalA(i, j) * u[j]
}
@@ -59,7 +59,7 @@ func (v Vec) Times(u Vec) {
func (v Vec) TimesTransp(u Vec) {
for i := 0; i < len(v); i++ {
- v[i] = 0;
+ v[i] = 0
for j := 0; j < len(u); j++ {
v[i] += evalA(j, i) * u[j]
}
@@ -67,27 +67,27 @@ func (v Vec) TimesTransp(u Vec) {
}
func (v Vec) ATimesTransp(u Vec) {
- x := make(Vec, len(u));
- x.Times(u);
- v.TimesTransp(x);
+ x := make(Vec, len(u))
+ x.Times(u)
+ v.TimesTransp(x)
}
func main() {
- flag.Parse();
- N := *n;
- u := make(Vec, N);
+ flag.Parse()
+ N := *n
+ u := make(Vec, N)
for i := 0; i < N; i++ {
u[i] = 1
}
- v := make(Vec, N);
+ v := make(Vec, N)
for i := 0; i < 10; i++ {
- v.ATimesTransp(u);
- u.ATimesTransp(v);
+ v.ATimesTransp(u)
+ u.ATimesTransp(v)
}
- var vBv, vv float64;
+ var vBv, vv float64
for i := 0; i < N; i++ {
- vBv += u[i] * v[i];
- vv += v[i] * v[i];
+ vBv += u[i] * v[i]
+ vv += v[i] * v[i]
}
- fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv));
+ fmt.Printf("%0.9f\n", math.Sqrt(vBv/vv))
}
diff --git a/test/bench/threadring.go b/test/bench/threadring.go
index c069a2655..031908a20 100644
--- a/test/bench/threadring.go
+++ b/test/bench/threadring.go
@@ -36,9 +36,9 @@ POSSIBILITY OF SUCH DAMAGE.
package main
import (
- "flag";
- "fmt";
- "os";
+ "flag"
+ "fmt"
+ "os"
)
var n = flag.Int("n", 1000, "how many passes")
@@ -47,25 +47,25 @@ const Nthread = 503
func f(i int, in <-chan int, out chan<- int) {
for {
- n := <-in;
+ n := <-in
if n == 0 {
- fmt.Printf("%d\n", i);
- os.Exit(0);
+ fmt.Printf("%d\n", i)
+ os.Exit(0)
}
- out <- n-1;
+ out <- n-1
}
}
func main() {
- flag.Parse();
+ flag.Parse()
- one := make(chan int); // will be input to thread 1
- var in, out chan int = nil, one;
+ one := make(chan int) // will be input to thread 1
+ var in, out chan int = nil, one
for i := 1; i <= Nthread-1; i++ {
- in, out = out, make(chan int);
- go f(i, in, out);
+ in, out = out, make(chan int)
+ go f(i, in, out)
}
- go f(Nthread, out, one);
- one <- *n;
- <-make(chan int); // hang until ring completes
+ go f(Nthread, out, one)
+ one <- *n
+ <-make(chan int) // hang until ring completes
}