Last active
August 29, 2015 14:21
-
-
Save timhudson/5ef8be71a28d781977f1 to your computer and use it in GitHub Desktop.
Super unscientific benchmark of while loops in Java and JavaScript
This file contains hidden or 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" | |
import "time" | |
func main() { | |
iterations := 1000000000 | |
count := 0 | |
start := time.Now().UnixNano() / 1000000 | |
for count < iterations { | |
count++ | |
} | |
duration := (time.Now().UnixNano() / 1000000) - start | |
fmt.Printf("%v iterations in %vms", count, duration) | |
} |
This file contains hidden or 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
public class While { | |
private static final long iterations = 1000000000; | |
private static long count = 0; | |
public static void main(String[] args) { | |
long start = System.currentTimeMillis(); | |
while (count < iterations * 10) { | |
count++; | |
} | |
long duration = System.currentTimeMillis() - start; | |
System.out.println(count + " iterations in " + duration + "ms"); | |
} | |
} |
This file contains hidden or 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
var iterations = 1000000000 | |
var count = 0 | |
var start = Date.now() | |
while (count < iterations) { | |
count++ | |
} | |
var duration = Date.now() - start | |
console.log(count + ' iterations in ' + duration + 'ms') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment