rust/src/test/bench/shootout-fasta.rs

95 lines
3.2 KiB
Rust
Raw Normal View History

/* -*- mode: rust; indent-tabs-mode: nil -*-
* Implementation of 'fasta' benchmark from
* Computer Language Benchmarks Game
* http://shootout.alioth.debian.org/
*/
use std;
2011-08-15 18:38:23 -05:00
import std::vec;
import std::str;
import std::uint;
import std::int;
fn LINE_LENGTH() -> uint { ret 60u; }
2011-07-27 07:19:39 -05:00
obj myrandom(mutable last: u32) {
fn next(mx: u32) -> u32 {
last = (last * 3877u32 + 29573u32) % 139968u32;
2011-07-27 07:19:39 -05:00
let ans = mx * last / 139968u32;
ret ans;
}
}
2011-07-27 07:19:39 -05:00
type aminoacids = {ch: char, prob: u32};
2011-08-11 23:37:27 -05:00
fn make_cumulative(aa: &[aminoacids]) -> [aminoacids] {
2011-07-27 07:19:39 -05:00
let cp: u32 = 0u32;
2011-08-11 23:37:27 -05:00
let ans: [aminoacids] = ~[];
for a: aminoacids in aa { cp += a.prob; ans += ~[{ch: a.ch, prob: cp}]; }
ret ans;
}
2011-08-11 23:37:27 -05:00
fn select_random(r: u32, genelist: &[aminoacids]) -> char {
2011-07-27 07:19:39 -05:00
if r < genelist.(0).prob { ret genelist.(0).ch; }
2011-08-11 23:37:27 -05:00
fn bisect(v: &[aminoacids], lo: uint, hi: uint, target: u32) -> char {
2011-07-27 07:19:39 -05:00
if hi > lo + 1u {
let mid: uint = lo + (hi - lo) / 2u;
if target < v.(mid).prob {
be bisect(v, lo, mid, target);
} else { be bisect(v, mid, hi, target); }
} else { ret v.(hi).ch; }
}
ret bisect(genelist, 0u, vec::len::<aminoacids>(genelist) - 1u, r);
}
2011-08-11 23:37:27 -05:00
fn make_random_fasta(id: str, desc: str, genelist: &[aminoacids], n: int) {
log ">" + id + " " + desc;
2011-07-27 07:19:39 -05:00
let rng = myrandom(std::rand::mk_rng().next());
let op: str = "";
for each i: uint in uint::range(0u, n as uint) {
str::push_byte(op, select_random(rng.next(100u32), genelist) as u8);
2011-07-27 07:19:39 -05:00
if str::byte_len(op) >= LINE_LENGTH() { log op; op = ""; }
}
2011-07-27 07:19:39 -05:00
if str::byte_len(op) > 0u { log op; }
}
2011-07-27 07:19:39 -05:00
fn make_repeat_fasta(id: str, desc: str, s: str, n: int) {
log ">" + id + " " + desc;
2011-07-27 07:19:39 -05:00
let op: str = "";
let sl: uint = str::byte_len(s);
for each i: uint in uint::range(0u, n as uint) {
str::push_byte(op, s.(i % sl));
2011-07-27 07:19:39 -05:00
if str::byte_len(op) >= LINE_LENGTH() { log op; op = ""; }
}
2011-07-27 07:19:39 -05:00
if str::byte_len(op) > 0u { log op; }
}
2011-07-27 07:19:39 -05:00
fn acid(ch: char, prob: u32) -> aminoacids { ret {ch: ch, prob: prob}; }
fn main(args: [str]) {
2011-08-11 23:37:27 -05:00
let iub: [aminoacids] =
make_cumulative(
~[acid('a', 27u32), acid('c', 12u32), acid('g', 12u32),
acid('t', 27u32), acid('B', 2u32), acid('D', 2u32),
acid('H', 2u32), acid('K', 2u32), acid('M', 2u32),
acid('N', 2u32), acid('R', 2u32), acid('S', 2u32),
acid('V', 2u32), acid('W', 2u32), acid('Y', 2u32)]);
let homosapiens: [aminoacids] =
make_cumulative(
~[acid('a', 30u32), acid('c', 20u32), acid('g', 20u32),
acid('t', 30u32)]);
2011-07-27 07:19:39 -05:00
let alu: str =
"GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG" +
"GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA" +
"CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT" +
"ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA" +
"GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG" +
"AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC" +
"AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
2011-07-27 07:19:39 -05:00
let n: int = 512;
make_repeat_fasta("ONE", "Homo sapiens alu", alu, n * 2);
make_random_fasta("TWO", "IUB ambiguity codes", iub, n * 3);
make_random_fasta("THREE", "Homo sapiens frequency", homosapiens, n * 5);
}