Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions bench/algorithm/coro-prime-sieve/1.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Single-thread implementation

actor Main
new create(env: Env) =>
let n: USize =
try
env.args(1)?.usize()?
else
100
end

let primes = Array[U64]
var count: USize = 0
var i: U64 = 2
while count < n do
var is_prime = true
for p in primes.values() do
if (p * p) > i then break end
if (i % p) == 0 then
is_prime = false
break
end
end
if is_prime then
env.out.write(i.string() + "\n")
primes.push(i)
count = count + 1
end
i = i + 1
end
96 changes: 96 additions & 0 deletions bench/algorithm/fannkuch-redux/1.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Based on the OCaml version from "The Computer Language Benchmarks Game"
// https://salsa.debian.org/benchmarksgame-team/benchmarksgame/

use "collections"

actor Main
new create(env: Env) =>
let n: USize = try env.args(1)?.usize()? else 7 end
Fannkuch(env, n)

class Fannkuch
let _env: Env
var _perm: Array[USize]
var _copy: Array[USize]
var _max_flips: USize = 0
var _checksum: ISize = 0
var _perm_count: USize = 0
let _n: USize

new create(env: Env, n: USize) =>
_env = env
_n = n
_perm = recover Array[USize](n) end
_copy = recover Array[USize](n) end

for i in Range[USize](0, n) do
_perm.push(i)
end

run()
print_results()

fun ref run() =>
// This is a direct translation of the recursive permutation
// generation from the OCaml version.
do_iter(_n)

fun ref do_iter(ht: USize) =>
if ht == 1 then
// Process the permutation
_copy.clear()
for i in Range[USize](0, _perm.size()) do
try _copy.push(_perm(i)?) end
end

let flips = count_flips(_copy)

// Update checksum: add for even permutations, subtract for odd
if (_perm_count % 2) == 0 then
_checksum = _checksum + flips.isize()
else
_checksum = _checksum - flips.isize()
end

if flips > _max_flips then
_max_flips = flips
end

_perm_count = _perm_count + 1
else
for i in Range[USize](0, ht) do
do_iter(ht - 1)
// Rotate the first `ht` elements
if ht > 1 then
try
let t = _perm(0)?
for j in Range[USize](1, ht) do
_perm(j-1)? = _perm(j)?
end
_perm(ht-1)? = t
end
end
end
end

fun ref count_flips(p: Array[USize]): USize =>
var flips: USize = 0
try
var first = p(0)?
while first != 0 do
// Flip the first `first + 1` elements
for i in Range[USize](0, (first / 2) + 1) do
let k = first - i
let t = p(i)?
p(i)? = p(k)?
p(k)? = t
end
flips = flips + 1
first = p(0)?
end
end
flips

fun ref print_results() =>
_env.out.print(_checksum.string())
_env.out.print("Pfannkuchen(" + _n.string() + ") = " + _max_flips.string())
107 changes: 107 additions & 0 deletions bench/algorithm/fasta/1.pony
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Based on the Rust (1.rs) implementation.

use "collections"

class RNG
var seed: U32
new create() =>
seed = 42
fun ref next(max: F64): F64 =>
seed = ((seed * 3877) + 29573) % 139968
(max * seed.f64()) / 139968.0

struct Amino
let l: U8
let p: F64
new create(l': U8, p': F64) =>
l = l'
p = p'

actor Main
new create(env: Env) =>
let n: USize = try env.args(1)?.usize()? else 10 end
let stdout = env.out
let rng = RNG
let alu: String val =
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGGGAGGCCGAGGCGGGCGGATCACCTGAGGTC" +
"AGGAGTTCGAGACCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAATACAAAAATTAGCCGGGCG" +
"TGGTGGCGCGCGCCTGTAATCCCAGCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGGAGGCGG" +
"AGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCCAGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA"

stdout.print(">ONE Homo sapiens alu")
try _repeat_and_wrap(stdout, alu, 2 * n)? end

let iub = recover iso Array[Amino](15)
.> push(Amino(97, 0.27)) .> push(Amino(99, 0.12)) .> push(Amino(103, 0.12)) .> push(Amino(116, 0.27))
.> push(Amino(66, 0.02)) .> push(Amino(68, 0.02)) .> push(Amino(72, 0.02)) .> push(Amino(75, 0.02))
.> push(Amino(77, 0.02)) .> push(Amino(78, 0.02)) .> push(Amino(82, 0.02)) .> push(Amino(83, 0.02))
.> push(Amino(86, 0.02)) .> push(Amino(87, 0.02)) .> push(Amino(89, 0.02))
end
stdout.print(">TWO IUB ambiguity codes")
try _generate_and_wrap(stdout, recover val consume iub end, 3 * n, rng)? end

let hs = recover iso Array[Amino](4)
.> push(Amino(97, 0.3029549426680)) .> push(Amino(99, 0.1979883004921))
.> push(Amino(103, 0.1975473066391)) .> push(Amino(116, 0.3015094502008))
end
stdout.print(">THREE Homo sapiens frequency")
try _generate_and_wrap(stdout, recover val consume hs end, 5 * n, rng)? end

fun _repeat_and_wrap(out: OutStream, seq: String val, count: USize) ? =>
let max_line: USize = 60
let slen = seq.size()
let sbytes: Array[U8] val = seq.array()
let padded = recover iso Array[U8](slen + max_line) end
var i: USize = 0
while i < (slen + max_line) do
padded.push(sbytes(i % slen)?)
i = i + 1
end
var off: USize = 0
var idx: USize = 0
while idx < count do
let rem = count - idx
let line_len = if rem < max_line then rem else max_line end
let line_arr = recover iso Array[U8](line_len) end
var t: USize = 0
while t < line_len do
line_arr.push(padded(off + t)?)
t = t + 1
end
out.print(String.from_array(consume line_arr))
off = off + line_len
if off >= slen then off = off - slen end
idx = idx + line_len
end

fun _generate_and_wrap(out: OutStream, nts: Array[Amino] val, count: USize, rng: RNG ref) ? =>
let max_line: USize = 60
var cum: F64 = 0
let cum_tot = recover iso Array[F64](nts.size()) end
var ni: USize = 0
while ni < nts.size() do
cum = cum + nts(ni)?.p
cum_tot.push(cum)
ni = ni + 1
end
var idx: USize = 0
while idx < count do
let rem = count - idx
let line_len = if rem < max_line then rem else max_line end
let line = recover iso Array[U8](line_len) end
var j: USize = 0
while j < line_len do
let r = rng.next(1.0)
var c: USize = 0
var ti: USize = 0
while ti < cum_tot.size() do
let t = cum_tot(ti)?
if r > t then c = c + 1 else break end
ti = ti + 1
end
line.push(nts(c)?.l)
j = j + 1
end
out.print(String.from_array(consume line))
idx = idx + line_len
end
Loading
Loading