From fae1143d1c1d41bd3b3f0ed3464dcab0eb1e2baf Mon Sep 17 00:00:00 2001 From: acheronfail Date: Tue, 19 Dec 2023 14:35:01 +1030 Subject: [PATCH] pass max value via cli --- .gitignore | 3 +++ count.asm | 28 ++++++++++++++++++++++------ count.c | 9 +++++---- count.cr | 3 ++- count.erl | 15 ++++++++------- count.f90 | 12 +++++++++--- count.go | 5 ++++- count.hs | 15 ++++++++++----- count.java | 6 +++--- count.js | 3 ++- count.kt | 15 ++++++++------- count.php | 3 ++- count.pl | 5 +++-- count.py | 7 +++++-- count.rb | 5 +++-- count.rs | 5 ++++- count.scala | 3 ++- count.zig | 9 ++++++++- justfile | 42 ++++++++++++++++++++++-------------------- 19 files changed, 125 insertions(+), 68 deletions(-) diff --git a/.gitignore b/.gitignore index a62a4ed..0db95df 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ build results +*.out +*.dump +*.beam *.class *.json *.o diff --git a/count.asm b/count.asm index 2e71c4d..934c060 100644 --- a/count.asm +++ b/count.asm @@ -6,20 +6,36 @@ extern printf global _start _start: - xor ecx, ecx + pop rdi ; argc is in rdi + pop rsi ; argv is in rsi + + add rsi, 8 ; skip the first argument + mov rdi, rsi ; move into rdi + xor rcx, rcx ; zero + xor rax, rax ; zero -start_loop: +parse_arg: + movzx rdx, byte [rdi + rcx] + sub rdx, '0' + imul rax, rax, 10 + add rax, rdx + inc rcx + cmp byte [rdi + rcx], 0 + jne parse_arg + + xor ecx, ecx +count: inc ecx - cmp ecx, 1000000000 - jl start_loop + cmp ecx, eax + jl count ; print mov rdi, format ; rdi: first argument - mov esi, ecx ; esi: second argument + mov esi, eax ; esi: second argument xor eax, eax ; number of vector registers used is 0 call printf - ; exit +end_program: mov eax, 60 ; system call number for exit xor edi, edi ; exit code 0 syscall diff --git a/count.c b/count.c index f9277f8..6ae78e5 100644 --- a/count.c +++ b/count.c @@ -1,9 +1,10 @@ #include +#include -int main() { +int main(int argc, char *argv[]) { int i = 0; - while (i < 1000000000) i++; + int target = atoi(argv[1]); + while(i < target) i++; printf("%d\n", i); - return 0; -} \ No newline at end of file +} diff --git a/count.cr b/count.cr index 608f601..6dbcf24 100644 --- a/count.cr +++ b/count.cr @@ -1,5 +1,6 @@ i = 0 -while i < 1_000_000_000 +target = ARGV[0]?.to_i?.not_nil! +while i < target i += 1 end diff --git a/count.erl b/count.erl index b894096..5c9017c 100644 --- a/count.erl +++ b/count.erl @@ -1,11 +1,12 @@ -module(count). --export([start/0, loop/1]). +-export([start/1, count/2]). -start() -> - loop(0). +start(Input) -> + Target = list_to_integer(atom_to_list(hd(Input))), + count(0, Target). -loop(N) when N < 1000000000 -> - loop(N+1); -loop(N) -> +count(N, T) when N < T -> + count(N+1, T); +count(N, _) -> io:fwrite("~B~n", [N]), - ok. + init:stop(0). diff --git a/count.f90 b/count.f90 index f10032a..d4d509e 100644 --- a/count.f90 +++ b/count.f90 @@ -1,8 +1,14 @@ program count - integer :: i - do while (i < 1000000000) + integer :: i, target + character(len=10) :: arg + + call getarg(1, arg) + read(arg,*) target + + i = 0 + do while (i < target) i = i + 1; end do print *, i -end program count \ No newline at end of file +end program count diff --git a/count.go b/count.go index 7909971..ea9fe0e 100644 --- a/count.go +++ b/count.go @@ -2,11 +2,14 @@ package main import ( "fmt" + "os" + "strconv" ) func main() { i := 0 - for i < 1000000000 { + target, _ := strconv.Atoi(os.Args[1]) + for i < target { i++ } fmt.Println(i) diff --git a/count.hs b/count.hs index e394518..db2d85a 100644 --- a/count.hs +++ b/count.hs @@ -1,7 +1,12 @@ -count :: Int -> IO () -count 1000000000 = print 1000000000 -count n = do - count (n + 1) +import System.Environment + +count :: Int -> Int -> IO () +count target n + | n == target = print n + | otherwise = count target (n + 1) main :: IO () -main = count 0 +main = do + [arg] <- getArgs + let target = read arg + count target 0 diff --git a/count.java b/count.java index 774d674..87537dd 100644 --- a/count.java +++ b/count.java @@ -1,9 +1,9 @@ public class count { - public static void main(String[] args){ int i = 0; - while (i < 1_000_000_000) { - i += 1; + int target = Integer.parseInt(args[0]); + while (i < target) { + i += 1; } System.out.println(i); diff --git a/count.js b/count.js index bcbc7c3..6bb9759 100644 --- a/count.js +++ b/count.js @@ -1,3 +1,4 @@ let i = 0; -while (i < 1_000_000_000) i++; +let target = parseInt(process.argv[2]); +while (i < target) i++; console.log(i); diff --git a/count.kt b/count.kt index 48d9e5f..89c3128 100644 --- a/count.kt +++ b/count.kt @@ -1,8 +1,9 @@ -fun main() { - var i = 0 - while (i < 1_000_000_000) { - i++; - } +fun main(args: Array) { + var i = 0 + val target = args[0].toInt() + while (i < target) { + i++ + } - println(i) -} + println(i) +} \ No newline at end of file diff --git a/count.php b/count.php index 26528db..e6482ae 100644 --- a/count.php +++ b/count.php @@ -1,6 +1,7 @@ ().unwrap(); + while i < target { i += 1; } diff --git a/count.scala b/count.scala index 6449dcc..448f1dd 100644 --- a/count.scala +++ b/count.scala @@ -1,7 +1,8 @@ object count { def main(args: Array[String]) = { var num = 0 - while (num < 1000000000) { + val target = args(0).toInt + while (num < target) { num += 1 } diff --git a/count.zig b/count.zig index 43f6038..f115b2d 100644 --- a/count.zig +++ b/count.zig @@ -1,7 +1,14 @@ const std = @import("std"); pub fn main() !void { + const allocator = std.heap.page_allocator; + var args = try std.process.argsAlloc(allocator); + defer std.process.argsFree(allocator, args); + + var target = try std.fmt.parseInt(u32, args[1], 10); var i: u32 = 0; - while (i < 1_000_000_000) : (i += 1) {} + + while (i < target) : (i += 1) {} + std.debug.print("{}\n", .{i}); } diff --git a/justfile b/justfile index 8b124ce..a0f9cb4 100644 --- a/justfile +++ b/justfile @@ -1,3 +1,5 @@ +i := '1000000000' + _default: just -l @@ -42,73 +44,73 @@ summary results: build-gcc: (_check "gcc") gcc -O3 ./count.c - echo './a.out' > CMD + echo './a.out {{i}}' > CMD build-clang: (_check "clang") clang -O3 ./count.c - echo './a.out' > CMD + echo './a.out {{i}}' > CMD build-rust: (_check "rustc") rustc -C opt-level=3 ./count.rs - echo './count' > CMD + echo './count {{i}}' > CMD build-fortran: (_check "gfortran") gfortran -O3 ./count.f90 - echo './a.out' > CMD + echo './a.out {{i}}' > CMD build-java: (_check "javac java") javac count.java - echo 'java count' > CMD + echo 'java count {{i}}' > CMD build-scala: (_check "scalac scala") scalac count.scala - echo 'scala count' > CMD + echo 'scala count {{i}}' > CMD build-kotlin: (_check "kotlinc java") kotlinc count.kt -include-runtime -d count.jar - echo 'java -jar count.jar' > CMD + echo 'java -jar count.jar {{i}}' > CMD build-ruby: (_check "ruby") - echo 'ruby count.rb' > CMD + echo 'ruby count.rb {{i}}' > CMD build-python3: (_check "python3") - echo 'python3 count.py' > CMD + echo 'python3 count.py {{i}}' > CMD build-node: (_check "node") - echo 'node count.js' > CMD + echo 'node count.js {{i}}' > CMD build-deno: (_check "deno") - echo 'deno run count.js' > CMD + echo 'deno run count.js {{i}}' > CMD build-bun: (_check "bun") - echo 'bun run count.js' > CMD + echo 'bun run count.js {{i}}' > CMD build-zig: (_check "zig") zig build-exe -O ReleaseFast ./count.zig - echo './count' > CMD + echo './count {{i}}' > CMD build-perl: (_check "perl") - echo 'perl ./count.pl' > CMD + echo 'perl ./count.pl {{i}}' > CMD build-haskell: (_check "ghc") ghc count.hs - echo './count' > CMD + echo './count {{i}}' > CMD build-go: (_check "go") go build -o count count.go - echo './count' > CMD + echo './count {{i}}' > CMD build-php: (_check "php") - echo 'php ./count.php' > CMD + echo 'php ./count.php {{i}}' > CMD build-erlang: (_check "erlc erl") erlc count.erl - echo 'erl -noshell -s count start -s init stop' > CMD + echo 'erl -noshell -s count start {{i}}' > CMD build-crystal: (_check "crystal") - echo 'crystal run ./count.cr' > CMD + echo 'crystal run ./count.cr {{i}}' > CMD build-assembly: (_check "nasm") nasm -f elf64 count.asm ld count.o -o count -lc -I/lib64/ld-linux-x86-64.so.2 - echo './count' > CMD + echo './count {{i}}' > CMD