Skip to content

Instantly share code, notes, and snippets.

@LaPingvino
Last active August 29, 2015 14:15
Show Gist options
  • Save LaPingvino/eeb6959e62a91f34840b to your computer and use it in GitHub Desktop.
Save LaPingvino/eeb6959e62a91f34840b to your computer and use it in GitHub Desktop.
My version of this code
// Recursion vs loops example in Go
package main
import (
"fmt"
"time"
)
const until = 42
func recursion(n int) {
fmt.Print(".")
n++
if n <= until {
recursion(n)
} else {
fmt.Println("done")
}
}
func loop(n int) {
for n <= until {
fmt.Print(".")
n++
}
fmt.Println("done")
}
func timeMethod(m string, f func(n int)) {
fmt.Printf("%s method:\n", m)
start := time.Now()
f(0)
duration := time.Since(start)
fmt.Println("Took", duration)
}
func main() {
timeMethod("Loop", loop)
timeMethod("Recursion", recursion)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment