From 37c4b1db9929a6a622fe2f61ad45031c398f7dbf Mon Sep 17 00:00:00 2001 From: zawz Date: Wed, 24 Jul 2024 14:02:49 +0200 Subject: [PATCH] Initial commit --- .gitignore | 4 + README.md | 7 ++ fib.c | 15 ++++ fib.cs | 16 ++++ fib.go | 20 +++++ fib.hs | 16 ++++ fib.java | 15 ++++ fib.js | 11 +++ fib.lua | 9 ++ fib.php | 13 +++ fib.py | 12 +++ fib.rb | 12 +++ fib.rs | 14 +++ perftest.sh | 242 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 406 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 fib.c create mode 100644 fib.cs create mode 100644 fib.go create mode 100644 fib.hs create mode 100644 fib.java create mode 100644 fib.js create mode 100644 fib.lua create mode 100644 fib.php create mode 100644 fib.py create mode 100644 fib.rb create mode 100644 fib.rs create mode 100755 perftest.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..944032e --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/build +/fib.build +/bin +/tmp diff --git a/README.md b/README.md new file mode 100644 index 0000000..08a61f5 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ + + +Usage: `./perftest.sh [files...] [-- N [args...]]` + +Example: +- all: `./perftest.sh -- 10 40` +- only fib.rs: `./perftest.sh fib.rs -- 10 40` diff --git a/fib.c b/fib.c new file mode 100644 index 0000000..cc38be7 --- /dev/null +++ b/fib.c @@ -0,0 +1,15 @@ +#include +#include + +long int fib(long int n) { + if (n<=1) { + return n; + } else { + return fib(n-1) + fib(n-2); + } +} + +int main(int arg, char** argv) { + printf("%ld\n", fib(atol(argv[1]))); + return 0; +} diff --git a/fib.cs b/fib.cs new file mode 100644 index 0000000..6a89962 --- /dev/null +++ b/fib.cs @@ -0,0 +1,16 @@ +using System; +class HelloWorld { + + static long fib(long n) { + if (n<=1) { + return n; + } else { + return fib(n-1) + fib(n-2); + } + } + + static void Main(string[] args) { + Console.WriteLine(fib(Int64.Parse(args[0]))); + } + +} \ No newline at end of file diff --git a/fib.go b/fib.go new file mode 100644 index 0000000..d60df55 --- /dev/null +++ b/fib.go @@ -0,0 +1,20 @@ +package main + +import ( + "os" + "strconv" + "fmt" +) + +func fib(n int) int { + if n <= 1 { + return n + } else { + return fib(n-1) + fib(n-2) + } +} + +func main() { + n,_ := strconv.Atoi(os.Args[1]) + fmt.Printf("%d\n", fib(n)) +} diff --git a/fib.hs b/fib.hs new file mode 100644 index 0000000..3a4d346 --- /dev/null +++ b/fib.hs @@ -0,0 +1,16 @@ +import System.Environment +import Data.List +import Text.Printf + +fib :: Int -> Int +fib 0 = 0 +fib 1 = 1 +fib n = fib (n-1) + fib (n-2) + +main :: IO () +main = do + args <- getArgs + case args of + [aInteger] | [(n,_)] <- reads aInteger -> + printf "%d\n" (fib n) + diff --git a/fib.java b/fib.java new file mode 100644 index 0000000..3fff61c --- /dev/null +++ b/fib.java @@ -0,0 +1,15 @@ +class Fib { + + public static int fib(int n) { + if (n <= 1) { + return n; + } else { + return fib(n-1) + fib(n-2); + } + } + + public static void main(String[] args) { + System.out.printf("%d\n", fib(Integer.parseInt(args[0]))); + } + +} diff --git a/fib.js b/fib.js new file mode 100644 index 0000000..0667792 --- /dev/null +++ b/fib.js @@ -0,0 +1,11 @@ + +function fib(n) { + if (n <= 1) { + return n + } else { + return fib(n-1) + fib(n-2) + } +} + +console.log(fib(Number(process.argv[2]))) + diff --git a/fib.lua b/fib.lua new file mode 100644 index 0000000..047e5b9 --- /dev/null +++ b/fib.lua @@ -0,0 +1,9 @@ +function fib (n) + if n <= 1 then + return n + else + return fib(n-1) + fib(n-2) + end +end + +print( fib(tonumber(arg[1])) ) diff --git a/fib.php b/fib.php new file mode 100644 index 0000000..8d8eb01 --- /dev/null +++ b/fib.php @@ -0,0 +1,13 @@ + diff --git a/fib.py b/fib.py new file mode 100644 index 0000000..dfb0a1f --- /dev/null +++ b/fib.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python3 + +import sys + +def fib(n: int) -> int: + if n <= 1: + return n + else: + return fib(n-1) + fib(n-2) + +if __name__ == '__main__': + print(fib(int(sys.argv[1]))) diff --git a/fib.rb b/fib.rb new file mode 100644 index 0000000..72dc2df --- /dev/null +++ b/fib.rb @@ -0,0 +1,12 @@ + + +def fib(n) + if (n <= 1) + return n + else + return fib(n-1) + fib(n-2) + end +end + +ans = fib(ARGV[0].to_i) +puts "#{ans}" diff --git a/fib.rs b/fib.rs new file mode 100644 index 0000000..de0a004 --- /dev/null +++ b/fib.rs @@ -0,0 +1,14 @@ +use std::env; + +fn fib(n: u64) -> u64{ + if n <= 1 { + n + } else { + fib(n-1) + fib(n-2) + } +} + +fn main() { + let args: Vec = env::args().collect(); + println!("{}", fib(args[1].parse().unwrap())); +} diff --git a/perftest.sh b/perftest.sh new file mode 100755 index 0000000..440c52a --- /dev/null +++ b/perftest.sh @@ -0,0 +1,242 @@ +#!/bin/bash + +[ "$DEBUG" = true ] && set -x + +trap 'exit 1' SIGINT + +mkdir -p bin + +build() { + local file=$1 ext= dst= + shift + ext=${file##*.} + dst=bin/${file%.*}-$ext + "$@" "$dst" "$file" >/dev/null + echo "$dst" +} + +build_wasm() { + local file=$1 ext= dst= + shift + ext=${file##*.} + dst=bin/${file%.*}-$ext.wasm + "$@" "$dst" "$file" >/dev/null + echo wasmtime "$dst" +} + +build_c() { + build "$1" gcc -O2 -o +} + +build_c_clang() { + build "$1" clang -O2 -o +} + +build_c_wasm() { + build_wasm "$1" emcc -O2 -o +} + +build_cpp() { + build "$1" g++ -O2 -o +} + +build_cpp_clang() { + build "$1" clang++ -O2 -o +} + +build_rs() { + build "$1" rustc -O -o +} + +build_rs_wasm() { + build_wasm "$1" rustc --target wasm32-wasi -O -o +} + +build_hs() { + build "$1" ghc -O -o + rm -f ${1%.*}.{o,hi} +} + +build_go() { + build "$1" go build -ldflags "-s -w" -o +} + +build_cs_mono() { + local file=$1 ext= dst= + shift + ext=${file##*.} + dst=bin/${file%.*}-$ext + mcs -out:"$dst" "$file" >/dev/null + echo "mono $dst" +} + +build_cs_dotnet() { + local file=$1 ext= dst= + shift + ext=${file##*.} + basename=${file%.*} + dst=bin/${file%.*}-$ext + tmppath=${TMPDIR-/tmp}/perftest_dotnetbuild/$basename + mkdir -p "$tmppath" + ( + cd "$tmppath" + dotnet new console >/dev/null + ) + cp "$file" "$tmppath"/Program.cs + ( + cd "$tmppath" + dotnet publish -c Release -p:PublishSingleFile=true -p:PublishTrimmed=true --self-contained=true >/dev/null + ) + cp "$tmppath"/bin/Release/*/*/publish/fib "$dst" + rm -rf /tmp/perftest_dotnetbuild/ + echo "$dst" +} + +unset _JAVA_OPTIONS +build_java() { + local file=$1 + shift + rm -rf build/java + mkdir -p build/java + javac -d build/java "$file" >/dev/null + class=$(echo build/java/*) + class=${class##*/} + class=${class%.*} + echo java -cp build/java "$class" "$@" +} + +# $@ = cmd +_time() { + /bin/time -f "%e" "$@" 2>&1 >/dev/null +} + +# $1 = N , $@ = cmd +measure() { + local N=$1 + shift + for I in $(seq 1 $N) ; do + _time "$@" + done | paste -sd+ | bc +} + +# $1 = file , $2 = cmd , $@ = args +run_wrap() { + local file=$1 cmd=$2 + shift 2 + echo $cmd "$file" "$@" +} + +# $1 = file , $2 = build fct , $@ = args +build_wrap() { + local file=$1 fct=$2 + shift 2 + dst=$("$fct" "$file") + echo "$dst $@" +} + +# $1 = N , $2 = file , $3 = fct , $@ = args +test_wrap() { + local N=$1 file=$2 fct=$3 + shift 3 + measured=$(measure "$N" $("$fct" "$file" "$@")) + echo "${file#./} $EXTRA_PRINT: $measured s" +} + +bincheck() { + for I + do + which "$I" &>/dev/null || return $? + done +} + +# $@ = keywords +skip() { + stat=0 + if [ -n "$ONLY" ] ; then + only=1 + fi + for word + do + if [ -n "$ONLY" ] && grep -qw "$word" <<< "$ONLY"; then + only=0 + fi + grep -qw "$word" <<< "$SKIP" && stat=$((stat+1)) + done + return $((stat+only)) +} + +files="" +while [ $# -gt 1 ] ; do + if [ "$1" = "--" ] ; then + shift + break + fi + files="$files +$1" + shift +done + +if [ -z "$files" ] ; then + files=$(find -maxdepth 1 -type f | sort) +fi + +N=10 +if [ $# -gt 0 ] ; then + N=$1 + shift +fi + +echo "$files" | while read -r file ; do + ext=${file##*.} + case $ext in + c) + bincheck gcc && EXTRA_PRINT="(gcc)" test_wrap "$N" "$file" build_wrap build_c "$@" + bincheck clang && EXTRA_PRINT="(clang)" test_wrap "$N" "$file" build_wrap build_c_clang "$@" + bincheck emcc wasmtime && + EXTRA_PRINT="(wasm)" test_wrap "$N" "$file" build_wrap build_c_wasm "$@" + ;; + cs) + bincheck mcs mono && EXTRA_PRINT="(mono)" test_wrap "$N" "$file" build_wrap build_cs_mono "$@" + bincheck dotnet && EXTRA_PRINT="(dotnet)" test_wrap "$N" "$file" build_wrap build_cs_dotnet "$@" + ;; + cpp) + bincheck g++ && EXTRA_PRINT="(g++)" test_wrap "$N" "$file" build_wrap build_cpp "$@" + bincheck clang++ && EXTRA_PRINT="(clang++)" test_wrap "$N" "$file" build_wrap build_cpp_clang "$@" + ;; + go) + bincheck go && test_wrap "$N" "$file" build_wrap build_go "$@" + ;; + hs) + bincheck ghc && test_wrap "$N" "$file" build_wrap build_hs "$@" + ;; + java) + bincheck javac && test_wrap "$N" "$file" build_java "$@" + ;; + js) + bincheck node && test_wrap "$N" "$file" run_wrap "node" "$@" + bincheck bun && EXTRA_PRINT="(bun)" test_wrap "$N" "$file" run_wrap "bun" "$@" + ;; + lua) + bincheck lua && test_wrap "$N" "$file" run_wrap "lua" "$@" + bincheck luajit && EXTRA_PRINT="(jit)" test_wrap "$N" "$file" run_wrap "luajit" "$@" + ;; + php) + bincheck php && test_wrap "$N" "$file" run_wrap "php -f" "$@" + ;; + py) + bincheck python && test_wrap "$N" "$file" run_wrap "python" "$@" + bincheck pypy && EXTRA_PRINT="(pypy)" test_wrap "$N" "$file" run_wrap "pypy" "$@" + ;; + rb) + bincheck ruby && RUBYPICK=_mri_ test_wrap "$N" "$file" run_wrap "ruby" "$@" + ;; + rs) + bincheck rustc && test_wrap "$N" "$file" build_wrap build_rs "$@" + bincheck rustc wasmtime && rustc --print target-list | grep -q '^wasm32-wasi$' && + EXTRA_PRINT="(wasm)" test_wrap "$N" "$file" build_wrap build_rs_wasm "$@" + ;; + wasm) + bincheck wasmtime && test_wrap "$N" "$file" run_wrap "wasmtime" "$@" + ;; + esac +done