Skip to content

Commit

Permalink
feat: add java
Browse files Browse the repository at this point in the history
Package name as `java` is not allow, hence the folder is called `jvm`.
  • Loading branch information
brunopacheco1 committed Nov 20, 2024
1 parent fd34795 commit f71f8c2
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.class
code
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ To run one of the benchmarks:
Bun = 0.83
Deno = 1.13
PyPy = 1.61
Java = 0.64
$
```

Expand All @@ -47,5 +48,3 @@ Emphasizes loop, conditional, and basic math performance.
## fibonacci

Emphasizes function call overhead and recursion.


1 change: 1 addition & 0 deletions clean.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rm c/code
rm go/code
rm jvm/code.class
1 change: 1 addition & 0 deletions compile.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
clang -O3 c/code.c -o c/code
go build -o go/code go/code.go
javac jvm/code.java
23 changes: 23 additions & 0 deletions fibonacci/jvm/code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package jvm;

public class code {

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

public static void main(String[] args) {
var u = Integer.parseInt(args[0]);
var r = 0;
for (var i = 1; i < u; i++) {
r += fibonacci(i);
}
System.out.println(r);
}
}
19 changes: 19 additions & 0 deletions loops/jvm/code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jvm;

import java.util.Random;

public class code {

public static void main(String[] args) {
var u = Integer.parseInt(args[0]); // Get an input number from the command line
var r = new Random().nextInt(10000); // Get a random number 0 <= r < 10k
var a = new int[10000]; // Array of 10k elements initialized to 0
for (var i = 0; i < 10000; i++) { // 10k outer loop iterations
for (var j = 0; j < 100000; j++) { // 100k inner loop iterations, per outer loop iteration
a[i] = a[i] + j % u; // Simple sum
}
a[i] += r; // Add a random value to each element in array
}
System.out.println(a[r]); // Print out a single element from the array
}
}
2 changes: 1 addition & 1 deletion run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ run "Node" "node ./js/code.js 40"
run "Bun" "bun ./js/code.js 40"
run "Deno" "deno ./js/code.js 40"
run "PyPy" "pypy ./py/code.py 40"

run "Java" "java jvm.code 40"

0 comments on commit f71f8c2

Please sign in to comment.