-
-
Save lunixbochs/00262cfc53c5096b57274768209f6aa6 to your computer and use it in GitHub Desktop.
This file contains 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 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