DEV Community

loading...

Discussion on: Ruby VS Python VS C++ VS JavaScript

Collapse
juancarlospaco profile image
Juan Carlos

Interesting post, Ruby 3 is JIT now,
but I read some post saying is really not faster for bigger projects.

Heres my solution and time, Nim lang code,
looks similar to Python or Ruby, runs like C or C++,
Nim compiles to JavaScript too (like TypeScript),
uses automatic deterministic compile-time memory management.

import random

proc do_the_thing(n: static int): int {.noinit, inline.} =
  var num = n  # Function arguments are immutable by default.
  var success, no_success = newSeqOfCap[int](num)
  while num > 0:
    if rand(1..num) >= num - 10:
      success.add num
    else:
      no_success.add num
    dec num  # Same as  num = num - 1
  result = success.len  # same as  result = success.len; return result

var sum = 0
for _ in 1..100:
  sum += do_the_thing(1000000)

echo sum
Enter fullscreen mode Exit fullscreen mode

Linux time says:

real    0m3,030s
user    0m3,030s
sys     0m0,010s
Enter fullscreen mode Exit fullscreen mode

🙂👍