Make for loops for efficient.

This commit is contained in:
Stephen Chung
2021-12-31 15:59:13 +08:00
parent 97900320e1
commit e3aa2c19ce
3 changed files with 85 additions and 18 deletions

26
scripts/for3.rhai Normal file
View File

@@ -0,0 +1,26 @@
const MAX = 100;
const CHECK = ((MAX - 1) ** 2) * MAX;
print("Ready... Go!");
let now = timestamp();
print(`Creating ${MAX} closures...`);
let list = [];
for i in 0..MAX {
list.push(|| i ** 2);
}
print(`Time = ${now.elapsed} seconds...`);
print(`Summing ${MAX} closures...`);
let sum = 0;
for f in list {
sum += f.call();
}
print(`Sum = ${sum} (should be ${CHECK})`);
print(`Finished. Total run time = ${now.elapsed} seconds.`);