Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active November 13, 2022 06:40
Show Gist options
  • Save diegofcornejo/9d746c8389ad52242e6929e7caf6b894 to your computer and use it in GitHub Desktop.
Save diegofcornejo/9d746c8389ad52242e6929e7caf6b894 to your computer and use it in GitHub Desktop.
Rust vs JavaScript - Sort array number

Rust vs JavaScript - Sort array number

Rust

This is a cargo project, you need to compile before use

cargo build --release

Or just use

cargo run --release -- 1000000

JavaScript

This project use Node 16 or later

node main.js 1000000 --max-old-space-size=7168

const { performance } = require('node:perf_hooks');
const args = process.argv.slice(2);
let n = parseInt(args[0]);
let numArray = [...Array(n).keys()];
let numbers = numArray.reverse();
console.log(`First: ${numbers[0]}, Last: ${numbers[numbers.length - 1]}`);
let start = performance.now();
numbers.sort(function (a, b) {
return a - b;
});
let end = performance.now();
console.log(`JS sorted: ${n} numbers in ${end - start} milliseconds`);
console.log(`First: ${numbers[0]}, Last: ${numbers[numbers.length - 1]}`);
use std::{env, time::Instant};
fn main() {
let n: u32 = env::args()
.nth(1)
.expect("Amount of numbers to sort not provided")
.parse()
.expect("First argument must be a positive number");
let mut numbers: Vec<u32> = (0..n).rev().collect();
println!("First: {}, last: {}", numbers.first().unwrap(), numbers.last().unwrap());
let start = Instant::now();
numbers.sort();
let duration = start.elapsed();
println!("Rust sorted: {n} numbers in {duration:?}");
println!("First: {}, last: {}", numbers.first().unwrap(), numbers.last().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment