Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardKelley authored Dec 8, 2024
2 parents f19fc1e + 6734f06 commit c9959d8
Show file tree
Hide file tree
Showing 35 changed files with 475 additions and 79 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ code
.idea/
*.user
*.o
.aider*
.env
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
https://benjdd.com/languages/
https://benjdd.com/languages/ and https://benjdd.com/languages2/

# Languages

Expand All @@ -11,27 +11,34 @@ If you want to add a language: PR!
To run one of the benchmarks:

1. `cd` into desired benchmark directory (EG `$ cd loops`)
2. Compile by running `$ bash ../compile.sh`
3. Run via `$ bash ../run.sh`.
2. Compile by running `$ ../compile.sh`
3. Run via `$ ../run.sh`.
You should see output something like:

```
$ bash ../run.sh
C = 0.77
Go = 2.07
Node = 0.79
Bun = 0.83
Deno = 1.13
PyPy = 1.61
Java = 0.64
$
```
$ ../run.sh
Benchmarking Zig
Benchmark 1: ./zig/code 40
Time (mean ± σ): 513.9 ms ± 2.9 ms [User: 504.5 ms, System: 2.6 ms]
Range (min … max): 510.6 ms … 516.2 ms 3 runs
4. For good measure, execute `$ bash ../clean.sh` when finished.
Benchmarking C
Benchmark 1: ./c/code 40
Time (mean ± σ): 514.0 ms ± 1.1 ms [User: 505.6 ms, System: 2.8 ms]
Range (min … max): 513.2 ms … 515.2 ms 3 runs
### Interpretation
The numbers represent the real execution time (wall-clock time) it took for each language to execute the given task. **A lower number indicates better performance.**
Benchmarking Rust
Benchmark 1: ./rust/target/release/code 40
Time (mean ± σ): 514.1 ms ± 2.0 ms [User: 504.6 ms, System: 3.1 ms]
Range (min … max): 512.4 ms … 516.3 ms 3 runs
...
```

4. For good measure, execute `$ ../clean.sh` when finished.

`bash ../run.sh` runs each program three times using the `runOnce` function and `awk` captures the real execution time.

Expand Down
11 changes: 10 additions & 1 deletion clean.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
rm c/code
rm cpp/code
rm go/code
rm jvm/code.class
rm -r rust/target
rm -rf kotlin/code.jar
rm kotlin/code.kexe
rm dart/code
rm -rf inko/build inko/code
rm nim/code
Expand All @@ -19,6 +21,13 @@ rm zig/code
rm lua/code
rm -f swift/code
rm haxe/code.jar
rm -rf csharp-aot/code
rm -rf csharp/code-aot
rm -rf csharp/code
rm -rf fsharp/code-aot
rm -rf fsharp/code
rm haskell/code haskell/*.hi haskell/*.o
rm v/code
rm emojicode/code emojicode/code.o
rm -f chez/code.so
rm -rf clojure/classes clojure/.cpcache
rm cobol/main
19 changes: 14 additions & 5 deletions compile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@ native-image -O3 jvm.code
RUSTFLAGS="-Zlocation-detail=none" cargo +nightly build --manifest-path rust/Cargo.toml --release
cargo build --manifest-path rust/Cargo.toml --release
kotlinc -include-runtime kotlin/code.kt -d kotlin/code.jar
#kotlinc-native -include-runtime kotlin/code.kt -d kotlin/code
kotlinc-native kotlin/code.kt -o kotlin/code -opt
dart compile exe dart/code.dart -o dart/code --target-os=macos
cd inko && inko build --opt=aggressive code.inko -o code && cd ..
nim c -d:danger --opt:speed nim/code.nim
nim c -d:danger --opt:speed -d:passC -x:off -a:off nim/code.nim
nim -d:release --threads:off --stackTrace:off --lineTrace:off --opt:speed -x:off -o:nim/code c nim/code.nim
sbcl --noinform --non-interactive --load "common-lisp/code.lisp" --build
fpc -O3 fpc/code.pas
crystal build -o crystal/code --release crystal/code.cr
#gnatmake -O3 -gnat2022 -gnatp -flto ada/code.adb -D ada -o ada/code
scala-cli --power package scala/code.scala -f -o scala/code
ldc2 -O3 -release -boundscheck=off d/code.d
ldc2 -O3 -release -boundscheck=off -mcpu=native flto=thin d/code.d
odin build odin/code.odin -o:speed -file -out:odin/code
clang -O3 -framework Foundation objc/code.m -o objc/code
gfortran -O3 fortran/code.f90 -o fortran/code
zig build-exe -O ReleaseFast -femit-bin=zig/code zig/code.zig
luajit -b lua/code.lua lua/code
swiftc -O -parse-as-library -Xcc -funroll-loops -Xcc -march=native -Xcc -ftree-vectorize -Xcc -ffast-math swift/code.swift -o swift/code
# haxe --class-path haxe -main Code --jvm haxe/code.jar # was getting errors running `haxelib install hxjava`
#dotnet publish csharp/csharp.csproj -o csharp/code-aot /p:PublishAot=true
dotnet publish csharp/csharp.csproj -o csharp/code
#dotnet publish csharp -o csharp/code-aot /p:PublishAot=true /p:OptimizationPreference=Speed
dotnet publish csharp -o csharp/code
#dotnet publish fsharp -o fsharp/code-aot /p:PublishAot=true /p:OptimizationPreference=Speed
dotnet publish fsharp -o fsharp/code
ghc -O2 -fllvm haskell/code.hs -o haskell/code || { echo "ghc: cannot compile with llvm backend; fallback to use default backend"; ghc -O2 haskell/code.hs -o haskell/code; }
v -prod -cc clang -d no_backtrace -gc none -o v/code v/code.v
emojicodec emojicode/code.emojic
echo '(compile-program "chez/code.ss")' | chez --optimize-level 3 -q
(cd clojure && mkdir -p classes && clojure -Sdeps '{:paths ["."]}' -M -e "(compile 'code)")
cobc -I /opt/homebrew/include/ -O -O2 -O3 -Os -x -o cobol/main cobol/main.cbl
lake build --dir lean4

24 changes: 24 additions & 0 deletions fibonacci/awk/code.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
BEGIN {

# Parse command-line argument for u
u = ARGV[1]
delete ARGV[1]

# Initialization not necessary, int(result) is zero by default

# Sum up Fibonacci values for numbers from 1 to u
for (i=1;i<u;i++) {
r += fibonacci(i)
}

# Print result
print r

}

# Recursive Fibonacci function
function fibonacci(n) {
if ( n == 0 ) { return 0 }
if ( n == 1 ) { return 1 }
return fibonacci(n - 1) + fibonacci(n - 2)
}
20 changes: 20 additions & 0 deletions fibonacci/chez/code.ss
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!chezscheme
(import (chezscheme))

(define (fib n)
(cond
[(fxzero? n) 0]
[(fx= 1 n) 1]
[else (fx+ (fib (fx- n 1))
(fib (fx- n 2)))]))

(define (main args)
(define u (string->number (cadr args)))
(display
(do [(i 1 (fx1+ i))
(r 0 (fx+ r (fib i)))]
[(>= i u)
r]))
(newline))

(main (command-line))
20 changes: 20 additions & 0 deletions fibonacci/clojure/code.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns code
(:gen-class))

(set! *unchecked-math* :warn-on-boxed)

(defn- fibonacci ^long [^long n]
((fn fib ^long [^long n]
(if (<= n 1)
n
(+ (long (fib (- n 1)))
(long (fib (- n 2)))))) n))

(defn -main [& args]
(let [u (long (parse-long (first args)))
r (loop [i 1
sum 0]
(if (< i u)
(recur (inc i) (+ sum (long (fibonacci i))))
sum))]
(println r)))
24 changes: 24 additions & 0 deletions fibonacci/cpp/code.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>
using namespace std;

int fibonacci(int n)
{
if (n == 0)
return 0;
if (n == 1)
return 1;
return fibonacci(n - 1) + fibonacci(n - 2);
}

int main(int argc, char *argv[])
{
int u = stoi(argv[1]);
int r = 0;
for (int i = 1; i < u; i++)
{
r += fibonacci(i);
}

cout << r << endl;
return 0;
}
File renamed without changes.
13 changes: 13 additions & 0 deletions fibonacci/fsharp/code.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[<EntryPoint>]
let main argv =
let rec fibonacci n =
if n < 2 then n
else fibonacci (n - 1) + fibonacci (n - 2)

let u = int argv[0]
let mutable r = 0
for i in 1..u-1 do
r <- r + fibonacci i

printfn $"{r}"
0
12 changes: 12 additions & 0 deletions fibonacci/fsharp/code.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<Compile Include="code.fs" />
</ItemGroup>

</Project>
25 changes: 16 additions & 9 deletions fibonacci/nim/code.nim
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import std/[strutils, os]
proc fibonacci(n: uint32): uint32 =
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
import std/[
strutils,
cmdline
]

let u: uint32 = paramStr(1).parseInt().uint32
var r: uint32
for i in 1..u:
proc fibonacci(n: uint64): uint64 =
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)

proc main() =
let u = uint64 parseUInt paramStr 1
var r: uint64 = 0
for i in 1..u:
r += fibonacci(i)
echo r
echo r

main()
17 changes: 17 additions & 0 deletions fibonacci/perl/code.pl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/usr/bin/env perl
use strict;
use warnings;

sub fibonacci {
my ($n) = @_;
return 0 if $n == 0;
return 1 if $n == 1;
return fibonacci($n-1) + fibonacci($n-2);
}

my $u = $ARGV[0];
my $r = 0;
for my $i (1..$u-1) {
$r += fibonacci($i);
}
print "$r\n";
15 changes: 9 additions & 6 deletions fibonacci/py/code.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import sys


def fibonacci(n):
if (n == 0):
return 0
if (n == 1):
return 1
return fibonacci(n-1) + fibonacci(n-2)
if n == 0:
return 0
if n == 1:
return 1
return fibonacci(n-1) + fibonacci(n-2)


def main():
u = int(sys.argv[1])
r = 0
for i in range(1, u):
r += fibonacci(i)
r += fibonacci(i)
print(r)


main()
11 changes: 8 additions & 3 deletions fibonacci/scala/code.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
//> using scala 3.6.1

package jvm

import scala.annotation.switch

@main
def main(number: String): Unit =
val u = number.toInt
var r = 0
for i <- 1 until u do
var i = 0
while i < u do
r += fibonacci(i)
i += 1
println(r)


def fibonacci(n: Int): Int =
n match
(n: @switch) match
case 0 => 0
case 1 => 1
case _ => fibonacci(n - 1) + fibonacci(n - 2)
19 changes: 19 additions & 0 deletions fibonacci/v/code.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn fibonacci(n i32) i32 {
if n == 0 {
return 0
}
if n == 1 {
return 1
}
return fibonacci(n - 1) + fibonacci(n - 2)
}

@[direct_array_access]
fn main() {
u := arguments()[1].i32()
mut r := i32(0)
for i := i32(1); i < u; i++ {
r += fibonacci(i)
}
println(r)
}
6 changes: 2 additions & 4 deletions fibonacci/zig/code.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub fn main() !void {
const u = try std.fmt.parseInt(usize, arg, 10);

var r: u64 = 0;
if (u > 0) {
for (1..u) |i| {
r += fib(i);
}
for (1..u) |i| {
r += fib(i);
}
try stdout.print("{d}\n", .{r});
}
Loading

0 comments on commit c9959d8

Please sign in to comment.