Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save tsujp/2d454015ec22d8a7a6dcb94f9ee493f5 to your computer and use it in GitHub Desktop.

Select an option

Save tsujp/2d454015ec22d8a7a6dcb94f9ee493f5 to your computer and use it in GitHub Desktop.

Revisions

  1. @s0kil s0kil revised this gist Jun 5, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Fibonacci_OCaml_Crystal_Go.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    # Fibonacci - OCaml vs Crystal
    # Fibonacci - Crystal vs OCaml vs Go

    ## Crystal

  2. @s0kil s0kil renamed this gist Jun 5, 2020. 1 changed file with 49 additions and 0 deletions.
    49 changes: 49 additions & 0 deletions Fibonacci_OCaml_Crystal.md → Fibonacci_OCaml_Crystal_Go.md
    Original file line number Diff line number Diff line change
    @@ -89,3 +89,52 @@ Runtime: 9.0s

    Conclusion: 9.1s
    ```

    ## Go

    Program: `fibonacci.go`

    ```go
    package main

    func fibonacci(n int) int {
    if n <= 1 {
    return n
    }
    return fibonacci(n-1) + fibonacci(n-2)
    }

    func main() {
    print(fibonacci(46))
    }
    ```

    Compile:

    ```bash
    $ time go build -o fibonacci-go fibonacci.go

    real 0m0.122s
    user 0m0.132s
    sys 0m0.045s
    ```

    Run:

    ```bash
    $ /usr/bin/time -v ./fibonacci-go
    1836311903
    Command being timed: "./fibonacci-go"
    User time (seconds): 12.50
    Percent of CPU this job got: 100%
    Maximum resident set size (kbytes): 1244
    ```

    Result:

    ```bash
    Compilation: 0.1s
    Runtime: 12.5s

    Conclusion: 12.6s
    ```
  3. @s0kil s0kil revised this gist Jun 5, 2020. 1 changed file with 43 additions and 2 deletions.
    45 changes: 43 additions & 2 deletions Fibonacci_OCaml_Crystal.md
    Original file line number Diff line number Diff line change
    @@ -44,7 +44,48 @@ Result:

    ```bash
    Compilation: 8.2s
    Runtime: 9.1s
    Runtime: 9.1s

    Conclusion: 17.3s
    ```
    ```

    ## OCaml

    Program: `fibonacci.ml`

    ```ocaml
    let rec fibonacci =
    function n -> if n < 2 then n else fibonacci (n - 1) + fibonacci (n - 2)
    ;;
    print_int (fibonacci(46))
    ```

    Compile:

    ```bash
    $ time ocamlopt -o fibonacci-ml fibonacci.ml
    real 0m0.082s
    user 0m0.065s
    sys 0m0.017s
    ```

    Run:

    ```bash
    $ /usr/bin/time -v ./fibonacci-ml
    1836311903
    Command being timed: "./fibonacci-ml"
    User time (seconds): 9.02
    Percent of CPU this job got: 99%
    Maximum resident set size (kbytes): 2308
    ```

    Result:

    ```bash
    Compilation: 0.1s
    Runtime: 9.0s

    Conclusion: 9.1s
    ```
  4. @s0kil s0kil revised this gist Jun 5, 2020. No changes.
  5. @s0kil s0kil renamed this gist Jun 5, 2020. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. @s0kil s0kil revised this gist Jun 5, 2020. No changes.
  7. @s0kil s0kil created this gist Jun 5, 2020.
    50 changes: 50 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,50 @@
    # Fibonacci - OCaml vs Crystal

    ## Crystal

    Program: `fibonacci.cr`

    ```crystal
    def fibonacci(n)
    return n if n < 2
    fibonacci(n - 1) + fibonacci(n - 2)
    end
    print fibonacci(46)
    ```

    Clean Cache:

    ```bash
    rm -rf ~/.cache/crystal/
    ```

    Compile:

    ```bash
    $ time crystal build --release -o fibonacci-cr fibonacci.cr

    real 0m8.205s
    user 0m8.317s
    sys 0m0.112s
    ```

    Run:

    ```bash
    $ /usr/bin/time -v ./fibonacci-cr
    1836311903
    Command being timed: "./fibonacci-cr"
    User time (seconds): 9.09
    Percent of CPU this job got: 99%
    Maximum resident set size (kbytes): 3192
    ```

    Result:

    ```bash
    Compilation: 8.2s
    Runtime: 9.1s

    Conclusion: 17.3s
    ```