Skip to content

Commit 85c4949

Browse files
committed
Elementary usage of template
1 parent 535c8c2 commit 85c4949

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed

src/tmplhtml/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Using template to update one value
2+
3+
Using a template when an elementary Fprint() suffices is costly.
4+
5+
`src>go test -bench=. ./tmplhtml`
6+
7+
**Results**
8+
9+
```
10+
go version go1.12.5 windows/amd64
11+
pkg: github.com/iWdGo/GoCompilerEfficiency/src/tmplhtml
12+
BenchmarkHTMLOutputTmpl-4 500000 3717 ns/op
13+
BenchmarkHTMLOutputBuffer-4 5000000 354 ns/op
14+
PASS
15+
```
16+
17+

src/tmplhtml/tmplhtml.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package tmplhtml
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"html/template"
7+
"log"
8+
)
9+
10+
const docPage = "efficiency"
11+
12+
var (
13+
tmpl = `<a href="http://golang.org/{{.V}}">documentation</a>`
14+
// 10x faster than parsing every time as elsewhere
15+
tmplT = template.Must(template.New("mytmpl").Parse(tmpl))
16+
)
17+
18+
type data = struct {
19+
V string
20+
}
21+
22+
// The test only executes the template with one variable value.
23+
func hTMLOutputTmpl(d *data) *bytes.Buffer {
24+
/* Passing the string or the struct does not change much the performance
25+
d := new(data)
26+
d.V = s
27+
28+
*/
29+
b := new(bytes.Buffer)
30+
if err := tmplT.Execute(b, d); err != nil {
31+
log.Fatalf("parse: %v", err)
32+
}
33+
return b
34+
}
35+
36+
func hTMLOutputBuffer(s string) *bytes.Buffer {
37+
b := new(bytes.Buffer)
38+
if _, err := fmt.Fprint(b, `<a href="http://golang.org/`+s+`">documentation</a>`); err != nil {
39+
log.Fatalf("fprint: %v", err)
40+
}
41+
return b
42+
}

src/tmplhtml/tmplhtml_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package tmplhtml
2+
3+
import "testing"
4+
5+
const (
6+
page = "efficiency"
7+
linkValue = `<a href="http://golang.org/efficiency">documentation</a>`
8+
)
9+
10+
func TestHTMLOutputTmpl(t *testing.T) {
11+
p := new(data)
12+
p.V = page
13+
for i := 0; i < 5; i++ {
14+
if got := hTMLOutputTmpl(p); got.String() != linkValue {
15+
t.Fatalf("want %s, got %s\n", linkValue, got)
16+
}
17+
}
18+
}
19+
20+
func TestHTMLOutputString(t *testing.T) {
21+
// For fairness but no change
22+
var s string
23+
s = page
24+
for i := 0; i < 5; i++ {
25+
if got := hTMLOutputBuffer(s); got.String() != linkValue {
26+
t.Fatalf("want %s, got %s\n", linkValue, got)
27+
}
28+
}
29+
}
30+
31+
// w/o the for loop, no benchmarking occurs because of optimization
32+
func BenchmarkHTMLOutputTmpl(b *testing.B) {
33+
p := new(data)
34+
p.V = page
35+
for n := 0; n < b.N; n++ {
36+
hTMLOutputTmpl(p)
37+
}
38+
}
39+
func BenchmarkHTMLOutputBuffer(b *testing.B) {
40+
for n := 0; n < b.N; n++ {
41+
hTMLOutputBuffer(page)
42+
}
43+
}

src/writestring/writestring.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ func ioWriteString(s string) *bytes.Buffer {
2626
}
2727
return b
2828
}
29+
30+
// TODO using defaunt print() after re-directing StdOut

0 commit comments

Comments
 (0)