Skip to content

Instantly share code, notes, and snippets.

@lunixbochs
Created September 1, 2017 04:10
Show Gist options
  • Save lunixbochs/00262cfc53c5096b57274768209f6aa6 to your computer and use it in GitHub Desktop.
Save lunixbochs/00262cfc53c5096b57274768209f6aa6 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"strings"
"time"
)
type withMessage struct {
msg string
cause *withMessage
}
func (w *withMessage) Quadratic() string {
if w == nil {
return ""
}
return w.msg + ": " + w.cause.Quadratic()
}
func (w *withMessage) Linear() string {
chain := []string{w.msg}
wc := w
for wc != nil {
chain = append(chain, wc.msg)
wc = wc.cause
}
return strings.Join(chain, ": ")
}
func main() {
wc := &withMessage{"msg", nil}
pos := wc
for i := 0; i < 20000; i++ {
pos = &withMessage{"msg", pos}
}
now := time.Now()
l := len(pos.Quadratic())
fmt.Println("quadratic", time.Since(now), l)
now = time.Now()
l = len(pos.Linear())
fmt.Println("linear", time.Since(now), l)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment