mirror of
https://github.com/pithings/unkernel.git
synced 2026-12-06 04:20:54 +08:00
24 lines
2.0 KiB
Raku
24 lines
2.0 KiB
Raku
# SPDX-License-Identifier: MIT
|
|
# Copyright (C) 2026 Pooya Parsa <pooya@pi0.io>
|
|
# Six small kernels in plain Perl (no module), from rangi's bench/compare/work.pl
|
|
# (the comparison with waarm64), with a third argument that picks the kernels
|
|
# to run (default all): test/work-bench.sh runs one per row.
|
|
# perl work.pl <size> <run's id> [kernels, e.g. 135]
|
|
$| = 1; $n = shift || 1; $id = shift || 0; $only = shift || "123456"; sub t { (times)[0] } print "B0:$id;\n"; $t = t;
|
|
sub k { my $u = t; printf "K%s.%s %.2f %s;\n", $id, $_[0], $u - $t, $_[1]; $t = $u }
|
|
# Numbers drawn by x = 16807 x mod 2^31 - 1: products under 2^46, exact where Perl's integers are 32 bits too.
|
|
sub r { $x = ($x * 16807) % 2147483647 }
|
|
# 1: integers and arrays (a sieve)
|
|
if (index($only, 1) >= 0) { $c = 0; for (1 .. 12 * $n) { @s = (1) x 30000; for $i (2 .. 173) { next unless $s[$i]; for ($j = $i * $i; $j < 30000; $j += $i) { $s[$j] = 0 } } $c += grep { $_ } @s } k(1, $c); }
|
|
# 2: strings and hashes (counting words)
|
|
if (index($only, 2) >= 0) { %h = (); $x = 1; for (1 .. 200000 * $n) { r; $h{"w" . ($x % 5000)}++ } k(2, scalar(keys %h) . "/" . $h{w42}); }
|
|
# 3: sorting numbers and strings
|
|
if (index($only, 3) >= 0) { $x = 7; @a = map { r } 1 .. 60000 * $n; @b = sort { $a <=> $b } @a; @d = sort map { "k$_" } @a; k(3, "$b[100]/$d[100]"); }
|
|
# 4: regular expressions
|
|
if (index($only, 4) >= 0) { $s = join " ", map { "item$_=" . ($_ * 37 % 1000) } 1 .. 4000; $c = 0; for (1 .. 12 * $n) { $c++ while $s =~ /item(\d+)=(\d*7)\b/g; ($r = $s) =~ s/(\d+)=/$1:/g; $c += length $r } k(4, $c); }
|
|
# 5: function calls (recursion)
|
|
if (index($only, 5) >= 0) { sub f { $_[0] < 2 ? $_[0] : f($_[0] - 1) + f($_[0] - 2) } $c = 0; $c += f(23) for 1 .. 2 * $n; k(5, $c); }
|
|
# 6: floating point (an escape-time picture)
|
|
if (index($only, 6) >= 0) { $c = 0; for $y (0 .. 59) { for $x (0 .. 119) { ($a, $b, $i) = (0, 0, 0); $u = $x / 40 - 2; $v = $y / 30 - 1; while ($i < 60 * $n && $a * $a + $b * $b < 4) { ($a, $b) = ($a * $a - $b * $b + $u, 2 * $a * $b + $v); $i++ } $c += $i } } k(6, $c); }
|
|
print "B9:$id;\n";
|