1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 /* -*- mode: rust; indent-tabs-mode: nil -*-
12 * Implementation of 'fasta' benchmark from
13 * Computer Language Benchmarks Game
14 * http://shootout.alioth.debian.org/
18 use std::io::{BufferedWriter, File};
22 static LINE_LENGTH: uint = 60;
23 static IM: u32 = 139968;
29 fn new() -> MyRandom { MyRandom { last: 42 } }
30 fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
31 fn gen(&mut self) -> u32 {
32 self.last = (self.last * 3877 + 29573) % IM;
38 rng: &'a mut MyRandom,
42 fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
45 .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
47 AAGen { rng: rng, data: data }
50 impl<'a> Iterator<u8> for AAGen<'a> {
51 fn next(&mut self) -> Option<u8> {
52 let r = self.rng.gen();
54 .skip_while(|pc| pc.val0() < r)
60 fn make_fasta<W: Writer, I: Iterator<u8>>(
61 wr: &mut W, header: &str, mut it: I, mut n: uint)
63 wr.write(header.as_bytes());
64 let mut line = [0u8, .. LINE_LENGTH + 1];
66 let nb = min(LINE_LENGTH, n);
67 for i in range(0, nb) {
68 line[i] = it.next().unwrap();
71 line[nb] = '\n' as u8;
72 wr.write(line.slice_to(nb + 1));
76 fn run<W: Writer>(writer: &mut W) {
77 let args = os::args();
78 let n = if os::getenv("RUST_BENCH").is_some() {
80 } else if args.len() <= 1u {
83 from_str(args[1]).unwrap()
86 let rng = &mut MyRandom::new();
88 "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
89 GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
90 CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
91 ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
92 GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
93 AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
94 AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
95 let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
96 ('t', 0.27), ('B', 0.02), ('D', 0.02),
97 ('H', 0.02), ('K', 0.02), ('M', 0.02),
98 ('N', 0.02), ('R', 0.02), ('S', 0.02),
99 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
100 let homosapiens = &[('a', 0.3029549426680),
101 ('c', 0.1979883004921),
102 ('g', 0.1975473066391),
103 ('t', 0.3015094502008)];
105 make_fasta(writer, ">ONE Homo sapiens alu\n",
106 alu.as_bytes().iter().cycle().map(|c| *c), n * 2);
107 make_fasta(writer, ">TWO IUB ambiguity codes\n",
108 AAGen::new(rng, iub), n * 3);
109 make_fasta(writer, ">THREE Homo sapiens frequency\n",
110 AAGen::new(rng, homosapiens), n * 5);
116 if os::getenv("RUST_BENCH").is_some() {
117 let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
120 run(&mut io::stdout());