Last active
August 29, 2015 14:15
-
-
Save LaPingvino/eeb6959e62a91f34840b to your computer and use it in GitHub Desktop.
My version of this code
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
// 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