Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Go loops #44

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compile.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
clang -O3 c/code.c -o c/code
go build -o go/code go/code.go
go build -ldflags "-s -w" -o go/code go/code.go
javac jvm/code.java
RUSTFLAGS="-Zlocation-detail=none" cargo +nightly build --manifest-path rust/Cargo.toml --release
kotlinc -include-runtime kotlin/code.kt -d kotlin/code.jar
Expand Down
10 changes: 5 additions & 5 deletions loops/go/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
func main() {
input, e := strconv.Atoi(os.Args[1]) // Get an input number from the command line
if e != nil { panic(e) }
u := int(input)
r := int(rand.Intn(10000)) // Get a random number 0 <= r < 10k
var a[10000]int // Array of 10k elements initialized to 0
for i := 0; i < 10000; i++ { // 10k outer loop iterations
for j := 0; j < 100000; j++ { // 100k inner loop iterations, per outer loop iteration
u := int32(input)
r := int32(rand.Intn(10000)) // Get a random number 0 <= r < 10k
var a[10000]int32 // Array of 10k elements initialized to 0
for i := int32(0); i < 10000; i++ { // 10k outer loop iterations
for j := int32(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
Expand Down