Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-19 04:54:17 -06:00
commit f086abcea6
789 files changed
+149185

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
#! @file iterators.sakura
#! @author John Christman (sorakatadzuma@gmail.com)
#! @copyright Malunal Studios, LLC.
#! @license MIT
#!
#! Contains the probe of what iterators cost: one sum, of the squares of the
#! even numbers among the first N, worked out by loops written out and by
#! adapters, over a vector and over a range, each ten times. Build it as an
#! executable, as its measure is of the code a compiler makes, and run it on
#! one core:
#!
#! sakura build --lib std=STD -O2 -o iterators tools/iterators.sakura
#! taskset -c 2 ./iterators [N]
#!
#! N is 10,000,000 where it is not given. The measure the iterators plan set is
#! the adapters given functors within 10% of `for &x in v`, which the last line
#! says.
import std::io;
import std::time;
#! Tests an element a view's iterator gives, a reference, by reference.
struct is_even : invokable<def(& &u64): bool> {}
def is_even::invoke(&mut this, x: & &u64): bool { return x % 2 == 0; }
#! Squares an element a view's iterator gives.
struct square : invokable<def(&u64): u64> {}
def square::invoke(&mut this, x: &u64): u64 { return x * x; }
#! Tests a number a range gives, by reference.
struct is_even_number : invokable<def(&u64): bool> {}
def is_even_number::invoke(&mut this, x: &u64): bool { return x % 2 == 0; }
#! Squares a number a range gives.
struct square_number : invokable<def(u64): u64> {}
def square_number::invoke(&mut this, x: u64): u64 { return x * x; }
def counted(n: u64): u64 {
var total: u64 = 0;
for x in 0..n {
if x % 2 == 0 { total += x * x; }
}
return total;
}
def referred(v: &vector<u64>): u64 {
var total: u64 = 0;
for &x in v {
if x % 2 == 0 { total += x * x; }
}
return total;
}
def indexed(v: &vector<u64>): u64 {
var total: u64 = 0;
for i in 0..v.length {
val x := v[i];
if x % 2 == 0 { total += x * x; }
}
return total;
}
def functors(v: &vector<u64>): u64 {
return v.iter().filter(is_even {}).map(square {}).sum();
}
def lambdas(v: &vector<u64>): u64 {
return v.iter().filter(def(x) => x % 2 == 0).map(def(x): u64 => x * x).sum();
}
def range_functors(n: u64): u64 {
return (0..n).filter(is_even_number {}).map(square_number {}).sum();
}
def range_lambdas(n: u64): u64 {
return (0..n).filter(def(x) => x % 2 == 0).map(def(x: u64): u64 => x * x).sum();
}
#! Runs a way ten times, writing how long it took and what it gave.
#! @param name What it is called.
#! @param run The way.
#! @returns How long it took, in nanoseconds.
def timed(name: strview, run: def(): u64): u64 {
val start := instant::now();
var sum: u64 = 0;
for _ in 0..10u {
sum += run();
}
val took := (instant::now() - start).in_nanoseconds as u64;
val ms := $"{took / 1000000}";
println($"{name.rpad(24, ' ')} {ms.lpad(6, ' ')} ms {sum}");
return took;
}
def main(args: span<strview>): void {
var n: u64 = 10000000;
if args.length > 0 {
n = integer::parse::<u64>(args[0]) else {
println("The count is a number, as 10000000.");
return;
};
}
var v := vector::<u64>::new();
v.reserve(n);
for i in 0..n {
v.append(i);
}
val r := &v;
timed("for x in 0..n", def(): u64 => counted(n));
val loop := timed("for &x in v", def(): u64 => referred(r));
timed("v[i]", def(): u64 => indexed(r));
val adapted := timed("adapters, functors", def(): u64 => functors(r));
timed("adapters, lambdas", def(): u64 => lambdas(r));
timed("range, functors", def(): u64 => range_functors(n));
timed("range, lambdas", def(): u64 => range_lambdas(n));
val ratio := adapted as f64 / (loop == 0 ? 1 : loop) as f64;
val verdict: strview = ratio <= 1.1 ? "within" : "outside";
println($"functors / for &x in v: {ratio}, {verdict} the measure of 1.1");
}