Last active
December 23, 2015 19:09
-
-
Save jmoiron/76d6afcd3bc49f30c18b to your computer and use it in GitHub Desktop.
Revisions
-
jmoiron revised this gist
Oct 19, 2014 . 2 changed files with 22 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -50,4 +50,7 @@ slow: fast @echo "" @python3 -V /usr/bin/time -p python3 ./montepi.py @echo "" @php --version |grep cli /usr/bin/time -p php ./montepi.php 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ <?php $iterations = isset($argv[1]) ? (int) $argv[1] : 100000000; $randmax = mt_getrandmax(); $hits = 0; function inCircle($x, $y) { return sqrt($x * $x + $y * $y) <= 1.0; } for ($i = 0; $i < $iterations; ++$i) { $x = mt_rand() / $randmax; $y = mt_rand() / $randmax; if (inCircle($x, $y)) { ++$hits; } } printf("Pi: %0.6f\n", (4 * ($hits / $iterations))); -
jmoiron revised this gist
Oct 19, 2014 . 1 changed file with 17 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ all: monte-c monte-go monte-rs monte-gccgo monte-go: go build montepi.go && mv montepi monte-go @@ -8,9 +8,18 @@ monte-rs: monte-c: gcc -std=c99 -O2 -o monte-c montepi.c -lm gcc -std=c99 -o monte-c-unop montepi.c -lm monte-gccgo: gccgo -g -O2 -c montepi.go gccgo -g -O2 -o monte-gccgo montepi.o rm -f montepi.o gccgo -g -c montepi.go gccgo -g -o monte-gccgo-unop montepi.o rm -f montepi.o clean: rm -f monte-c monte-go monte-rs monte-gccgo monte-gccgo-unop monte-c-unop fast: all @gcc --version 2>/dev/null |grep -E "(gcc|LLVM)" @@ -19,6 +28,12 @@ fast: all @go version /usr/bin/time -p ./monte-go @echo "" @echo gccgo --version |grep "gcc" /usr/bin/time -p ./monte-gccgo @echo "" @echo gccgo --version |grep "gcc" /usr/bin/time -p ./monte-gccgo-unop @echo "" @rustc --version /usr/bin/time -p ./monte-rs -
jmoiron revised this gist
Oct 15, 2014 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -13,17 +13,26 @@ clean: rm -f monte-c monte-go monte-rs fast: all @gcc --version 2>/dev/null |grep -E "(gcc|LLVM)" /usr/bin/time -p ./monte-c @echo "" @go version /usr/bin/time -p ./monte-go @echo "" @rustc --version /usr/bin/time -p ./monte-rs slow: fast @echo "" @ruby --version /usr/bin/time -p ./montepi.rb @echo "" @python -V /usr/bin/time -p ./montepi.py @echo "" @pypy -V /usr/bin/time -p pypy ./montepi.py @echo "" @python3 -V /usr/bin/time -p python3 ./montepi.py -
jmoiron revised this gist
Oct 15, 2014 . 3 changed files with 52 additions and 9 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,29 @@ all: monte-c monte-go monte-rs monte-go: go build montepi.go && mv montepi monte-go monte-rs: rustc -O -o monte-rs montepi.rs monte-c: gcc -std=c99 -O2 -o monte-c montepi.c -lm clean: rm -f monte-c monte-go monte-rs fast: all /usr/bin/time -p ./monte-c /usr/bin/time -p ./monte-go /usr/bin/time -p ./monte-rs slow: fast ruby --version /usr/bin/time -p ./montepi.rb python -V /usr/bin/time -p ./montepi.py pypy -V /usr/bin/time -p pypy ./montepi.py python3 -V /usr/bin/time -p python3 ./montepi.py 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,10 @@ montecarlo pi estimations in various languages. Just a bit of fun. Requires: * rustc * go * gcc * python2 * pypy * python3 * ruby 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,22 @@ #!/usr/bin/env python from time import time from random import random, seed from math import sqrt seed(time()) iterations = 100000000 if 'xrange' not in dir(__builtins__): __builtins__.xrange = range def in_circle(x, y): return sqrt(x*x + y*y) <= 1.0 hits = 0 for i in xrange(1, iterations): if in_circle(random(), random()): hits += 1 pi = 4 * (hits / float(iterations)) print("pi = %0.6f" % (pi)) -
jmoiron revised this gist
Oct 15, 2014 . 2 changed files with 17 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -15,3 +15,4 @@ time: /usr/bin/time -p ./monte-c /usr/bin/time -p ./monte-go /usr/bin/time -p ./monte-rs /usr/bin/time -p ./montepi.rb 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ #!/usr/bin/env ruby def in_circle(x, y) return Math.hypot(x, y) <= 1.0 end iterations = 100000000 h = 0 (0..iterations).each do |i| if in_circle(rand(), rand()) h+=1 end end pi = 4.0 * h / iterations.to_f puts "Pi: #{pi}" -
jmoiron revised this gist
Oct 15, 2014 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ all: gcc -std=c99 -O2 -o monte-c montepi.c -lm go build montepi.go && mv montepi monte-go rustc -O -o monte-rs montepi.rs @@ -8,10 +8,10 @@ clean: time: echo "Build" /usr/bin/time -p gcc -std=c99 -O2 -o monte-c montepi.c -lm /usr/bin/time -p go build montepi.go && mv montepi monte-go /usr/bin/time -p rustc -O -o monte-rs montepi.rs echo "Run" /usr/bin/time -p ./monte-c /usr/bin/time -p ./monte-go /usr/bin/time -p ./monte-rs -
jmoiron revised this gist
Oct 15, 2014 . 2 changed files with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ all: gcc -O2 -o monte-c montepi.c go build montepi.go && mv montepi monte-go rustc -O -o monte-rs montepi.rs @@ -8,7 +8,7 @@ clean: time: echo "Build" /usr/bin/time gcc -O2 -o monte-c montepi.c /usr/bin/time go build montepi.go && mv montepi monte-go /usr/bin/time rustc -O -o monte-rs montepi.rs echo "Run" 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ /* montecarlo approximation of pi in c */ #include <stdio.h> #include <time.h> #include <stdlib.h> @@ -28,4 +28,4 @@ int main() { } printf("Pi: %0.6f\n", (4 * ((float)hits / ITERATIONS))); return 0; } -
jmoiron revised this gist
Oct 15, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ all: gcc -O3 -o monte-c montepi.c go build montepi.go && mv montepi monte-go rustc -O -o monte-rs montepi.rs @@ -8,7 +8,7 @@ clean: time: echo "Build" /usr/bin/time gcc -O3 -o monte-c montepi.c /usr/bin/time go build montepi.go && mv montepi monte-go /usr/bin/time rustc -O -o monte-rs montepi.rs echo "Run" -
jmoiron revised this gist
Oct 15, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ all: gcc -o monte-c montepi.c go build montepi.go && mv montepi monte-go rustc -O -o monte-rs montepi.rs clean: rm -f monte-c monte-go monte-rs @@ -10,7 +10,7 @@ time: echo "Build" /usr/bin/time gcc -o monte-c montepi.c /usr/bin/time go build montepi.go && mv montepi monte-go /usr/bin/time rustc -O -o monte-rs montepi.rs echo "Run" /usr/bin/time ./monte-c /usr/bin/time ./monte-go -
jmoiron revised this gist
Oct 15, 2014 . 2 changed files with 23 additions and 6 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,17 @@ all: gcc -o monte-c montepi.c go build montepi.go && mv montepi monte-go rustc -o monte-rs montepi.rs clean: rm -f monte-c monte-go monte-rs time: echo "Build" /usr/bin/time gcc -o monte-c montepi.c /usr/bin/time go build montepi.go && mv montepi monte-go /usr/bin/time rustc -o monte-rs montepi.rs echo "Run" /usr/bin/time ./monte-c /usr/bin/time ./monte-go /usr/bin/time ./monte-rs 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,7 @@ // montecarlo approximation of pi in rust use std::rand; use std::rand::{Rng, XorShiftRng}; fn in_circle(x :f64, y :f64) -> bool { let f = (x*x + y*y).sqrt(); @@ -10,18 +10,18 @@ fn in_circle(x :f64, y :f64) -> bool { fn main() { let iterations = 100000000i; let mut rng: XorShiftRng = rand::random(); let mut hits = 0i; for _ in range(0i, iterations) { let x = rng.gen::<f64>(); let y = rng.gen::<f64>(); if in_circle(x, y) { hits+=1; } } let pi :f64 = 4.0 * hits as f64 / iterations as f64; println!("Pi: {}", pi); } -
jmoiron created this gist
Oct 15, 2014 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ /* monte carlo estimation of pi in c */ #include <stdio.h> #include <time.h> #include <stdlib.h> #include <math.h> #define ITERATIONS 100000000 int in_circle(float x, float y) { if (sqrt(x*x + y*y) <= 1.0) { return 1; } return 0; } int main() { srand(time(NULL)); rand(); int hits=0; for (int i=0; i<ITERATIONS; i++) { float r1 = (float)rand()/(float)RAND_MAX; float r2 = (float)rand()/(float)RAND_MAX; if (in_circle(r1, r2)) { hits++; } } printf("Pi: %0.6f\n", (4 * ((float)hits / ITERATIONS))); return 0; } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ // montecarlo approximation of pi in go package main import ( "math" "math/rand" "time" ) const iterations = 100000000 func inCircle(x, y float64) bool { return math.Sqrt(x*x+y*y) <= 1.0 } func main() { source := rand.NewSource(time.Now().Unix()) r := rand.New(source) var h int for i := 0; i <= iterations; i++ { if inCircle(r.Float64(), r.Float64()) { h++ } } pi := 4 * float64(h) / float64(iterations) println("Pi:", pi) } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ // montecarlo approximation of pi in rust use std::rand; use std::rand::Rng; fn in_circle(x :f64, y :f64) -> bool { let f = (x*x + y*y).sqrt(); f <= 1.0 } fn main() { let iterations = 100000000i; let mut rng = rand::task_rng(); let mut hits = 0i; for _ in range(0i, 100000000i) { let x = rng.gen::<f64>(); let y = rng.gen::<f64>(); if in_circle(x, y) { hits+=1; } } let pi :f64 = 4.0 * hits as f64 / iterations as f64; println!("Pi: {}", pi); }