Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

L-systems in Go

View lsystems.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
package lsystems
 
import (
"bytes"
)
 
func lsystem_buffer(s *bytes.Buffer, rules []string) {
tmpbuf := bytes.NewBuffer(make([]byte, 0, 1e6))
 
for _, b := range s.Bytes() {
if rule := rules[b]; rule != "" {
tmpbuf.WriteString(rule)
} else {
tmpbuf.WriteByte(b)
}
}
 
*s = *tmpbuf
}
 
func lsystem_append(s *[]byte, rules []string) {
tmpbuf := make([]byte, 0, 1e6)
 
for _, b := range *s {
if rule := rules[b]; rule != "" {
tmpbuf = append(tmpbuf, rule...)
} else {
tmpbuf = append(tmpbuf, b)
}
}
 
*s = tmpbuf
}
View lsystems.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 59 60 61 62
package lsystems
 
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
 
type rule struct {
key []byte
value []byte
}
 
func BenchmarkBuffer(b *testing.B) {
b.StopTimer()
rules_in := []byte("{\"X\": \"F-[[X]+X]+F[+FX]-X\", \"F\": \"FF\"}")
rules_tmp := make(map[string]string)
json.Unmarshal(rules_in, &rules_tmp)
 
rules := make([]string, 256)
for key, value := range rules_tmp {
rules[byte(key[0])] = value
}
 
out := bytes.NewBuffer(make([]byte, 0, 1e8))
 
b.StartTimer()
for i := 0; i < b.N; i++ {
out.Reset()
out.WriteString("X")
for x := 0; x < 9; x++ {
lsystem_buffer(out, rules)
}
}
 
fmt.Println(out.String())
}
 
func BenchmarkAppend(b *testing.B) {
b.StopTimer()
rules_in := []byte("{\"X\": \"F-[[X]+X]+F[+FX]-X\", \"F\": \"FF\"}")
rules_tmp := make(map[string]string)
json.Unmarshal(rules_in, &rules_tmp)
 
rules := make([]string, 256)
for key, value := range rules_tmp {
rules[byte(key[0])] = value
}
 
out := make([]byte, 0, 1e8)
 
b.StartTimer()
for i := 0; i < b.N; i++ {
out = []byte("X")
for x := 0; x < 9; x++ {
lsystem_append(&out, rules)
}
}
 
fmt.Println(string(out))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.