Skip to content

Instantly share code, notes, and snippets.

@jmoiron
Last active December 23, 2015 19:09
Show Gist options
  • Save jmoiron/76d6afcd3bc49f30c18b to your computer and use it in GitHub Desktop.
Save jmoiron/76d6afcd3bc49f30c18b to your computer and use it in GitHub Desktop.

Revisions

  1. jmoiron revised this gist Oct 19, 2014. 2 changed files with 22 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions Makefile
    Original 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

    19 changes: 19 additions & 0 deletions montepi.php
    Original 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)));
  2. jmoiron revised this gist Oct 19, 2014. 1 changed file with 17 additions and 2 deletions.
    19 changes: 17 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    all: monte-c monte-go monte-rs
    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
    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

  3. jmoiron revised this gist Oct 15, 2014. 1 changed file with 14 additions and 5 deletions.
    19 changes: 14 additions & 5 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -13,17 +13,26 @@ clean:
    rm -f monte-c monte-go monte-rs

    fast: all
    /usr/bin/time -p ./monte-c
    @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
    ruby --version
    @echo ""
    @ruby --version
    /usr/bin/time -p ./montepi.rb
    python -V
    @echo ""
    @python -V
    /usr/bin/time -p ./montepi.py
    pypy -V
    @echo ""
    @pypy -V
    /usr/bin/time -p pypy ./montepi.py
    python3 -V
    @echo ""
    @python3 -V
    /usr/bin/time -p python3 ./montepi.py

  4. jmoiron revised this gist Oct 15, 2014. 3 changed files with 52 additions and 9 deletions.
    29 changes: 20 additions & 9 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,18 +1,29 @@
    all:
    gcc -std=c99 -O2 -o monte-c montepi.c -lm
    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

    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
    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

    10 changes: 10 additions & 0 deletions README.md
    Original 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
    22 changes: 22 additions & 0 deletions montepi.py
    Original 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))
  5. jmoiron revised this gist Oct 15, 2014. 2 changed files with 17 additions and 0 deletions.
    1 change: 1 addition & 0 deletions Makefile
    Original 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
    16 changes: 16 additions & 0 deletions montepi.rb
    Original 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}"
  6. jmoiron revised this gist Oct 15, 2014. 1 changed file with 7 additions and 7 deletions.
    14 changes: 7 additions & 7 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    all:
    gcc -O2 -o monte-c montepi.c
    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 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
    /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 ./monte-c
    /usr/bin/time ./monte-go
    /usr/bin/time ./monte-rs
    /usr/bin/time -p ./monte-c
    /usr/bin/time -p ./monte-go
    /usr/bin/time -p ./monte-rs
  7. jmoiron revised this gist Oct 15, 2014. 2 changed files with 4 additions and 4 deletions.
    4 changes: 2 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    all:
    gcc -O3 -o monte-c montepi.c
    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 -O3 -o monte-c montepi.c
    /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"
    4 changes: 2 additions & 2 deletions montepi.c
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    /* monte carlo estimation of pi in c */
    /* 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;
    }
    }
  8. jmoiron revised this gist Oct 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Makefile
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    all:
    gcc -o monte-c montepi.c
    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 -o monte-c montepi.c
    /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"
  9. jmoiron revised this gist Oct 15, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions Makefile
    Original 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 monte-rs montepi.rs
    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 monte-rs montepi.rs
    /usr/bin/time rustc -O -o monte-rs montepi.rs
    echo "Run"
    /usr/bin/time ./monte-c
    /usr/bin/time ./monte-go
  10. jmoiron revised this gist Oct 15, 2014. 2 changed files with 23 additions and 6 deletions.
    17 changes: 17 additions & 0 deletions Makefile
    Original 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
    12 changes: 6 additions & 6 deletions montepi.rs
    Original 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;
    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 = rand::task_rng();
    let mut rng: XorShiftRng = rand::random();
    let mut hits = 0i;

    for _ in range(0i, 100000000i) {
    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);
    }
    }
  11. jmoiron created this gist Oct 15, 2014.
    31 changes: 31 additions & 0 deletions montepi.c
    Original 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;
    }
    27 changes: 27 additions & 0 deletions montepi.go
    Original 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)
    }
    27 changes: 27 additions & 0 deletions montepi.rs
    Original 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);
    }