1 Billion nested loop iterations




Methodology

Ran each three times and used the lowest timing for each. Timings taken on a g-2vcpu-8gb digital ocean instance using the time command. Input value of 10 given to each.

Python

import sys
import random
u = int(sys.argv[1])         # Get an input number from the command line
r = random.randint(0, 10000) # Get a random number 0 <= r < 10k
a  = [0] * 10000             # Array of 10k elements initialized to 0
for i in range(10000):       # 10k outer loop iterations
  for j in range(100000):    # 100k inner loop iterations, per outer loop iteration
    a[i] = a[i] + u          # Simple sum
  a[i] = a[i] + r            # Add a random value to each element in array
print(a[r])                  # Print out a single element from the arra

Javascript (Node and Bun)

var u = Number(process.argv[2]);           // Get an input number from the command line
var r = Math.floor(Math.random() * 10000); // Get a random number 0 <= r < 10k
var a = new Int32Array(10000);             // Array of 10k elements initialized to 0
for (let i = 0; i < 10000; i++) {          // 10k outer loop iterations
  for (let j = 0; j < 100000; j++) {       // 100k inner loop iterations, per outer loop iteration
    a[i] = a[i] + u;                       // Simple sum
  }
  a[i] += r;                               // Add a random value to each element in array
}
console.log(a[r]);                         // Print out a single element from the array

Go

package main
import (
    "fmt"
    "math/rand"
    "strconv"
    "os"
)
func main() {
  input, e := strconv.Atoi(os.Args[1]) // Get an input number from the command line
  if e != nil { panic(e) }
  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 := range a { a[i] = 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
      a[i] = a[i] + u                  // Simple sum
    }
    a[i] += r                          // Add a random value to each element in array
  }
  fmt.Println(a[r])                    // Print out a single element from the array
}

C

#include "stdio.h"
#include "stdlib.h"
#include "stdint.h"
int main (int argc, char** argv) {
  int u = atoi(argv[1]);               // Get an input number from the command line
  int r = rand() % 10000;              // Get a random integer 0 <= r < 10k
  int32_t a[10000] = {0};              // Array of 10k elements initialized to 0
  for (int i = 0; i < 10000; i++) {    // 10k outer loop iterations
    for (int j = 0; j < 100000; j++) { // 100k inner loop iterations, per outer loop iteration
      a[i] = a[i] + u;                 // Simple sum
    }
    a[i] += r;                         // Add a random value to each element in array
  }
  printf("%d\n", a[r]);                // Print out a single element from the array
}