Skip to content

Commit

Permalink
pass max value via cli
Browse files Browse the repository at this point in the history
  • Loading branch information
acheronfail committed Dec 19, 2023
1 parent fae1be1 commit fae196b
Show file tree
Hide file tree
Showing 20 changed files with 129 additions and 68 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
build
results
*.out
*.dump
*.beam
*.class
*.json
*.o
Expand Down
28 changes: 22 additions & 6 deletions count.asm
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 5 additions & 4 deletions count.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <stdio.h>
#include <stdlib.h>

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;
}
}
3 changes: 2 additions & 1 deletion count.cr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 4 additions & 0 deletions count.deno
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
let i = 0;
let target = parseInt(Deno.args[0]);
while (i < target) i++;
console.log(i);
15 changes: 8 additions & 7 deletions count.erl
Original file line number Diff line number Diff line change
@@ -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).
12 changes: 9 additions & 3 deletions count.f90
Original file line number Diff line number Diff line change
@@ -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
end program count
5 changes: 4 additions & 1 deletion count.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 10 additions & 5 deletions count.hs
Original file line number Diff line number Diff line change
@@ -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
6 changes: 3 additions & 3 deletions count.java
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
3 changes: 2 additions & 1 deletion count.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 8 additions & 7 deletions count.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
fun main() {
var i = 0
while (i < 1_000_000_000) {
i++;
}
fun main(args: Array<String>) {
var i = 0
val target = args[0].toInt()
while (i < target) {
i++
}

println(i)
}
println(i)
}
3 changes: 2 additions & 1 deletion count.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
$i = 0;
while ($i < 1000000000) {
$target = (int) $argv[1];
while ($i < $target) {
$i++;
}
echo $i . "\n";
Expand Down
5 changes: 3 additions & 2 deletions count.pl
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
my $i = 0;
for ($i = 0; $i < 1000000000; $i++) {}
print "$i\n";
my $limit = $ARGV[0] + 0;
for ($i = 0; $i < $limit; $i++) {}
print "$i\n";
7 changes: 5 additions & 2 deletions count.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sys

i = 0
while i < 1000000000:
target = int(sys.argv[1])
while i < target:
i += 1

print(i)
print(i)
5 changes: 3 additions & 2 deletions count.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
i = 0
while i < 1_000_000_000 do
target = ARGV[0].to_i
while i < target do
i += 1
end

puts i
puts i
5 changes: 4 additions & 1 deletion count.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::env;

fn main() {
let mut i = 0;
while i < 1_000_000_000 {
let target = env::args().nth(1).unwrap().parse::<i32>().unwrap();
while i < target {
i += 1;
}

Expand Down
3 changes: 2 additions & 1 deletion count.scala
Original file line number Diff line number Diff line change
@@ -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
}

Expand Down
9 changes: 8 additions & 1 deletion count.zig
Original file line number Diff line number Diff line change
@@ -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});
}
42 changes: 22 additions & 20 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
i := '1000000000'

_default:
just -l

Expand Down Expand Up @@ -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.deno {{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

0 comments on commit fae196b

Please sign in to comment.