]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-fasta.rs
auto merge of #10927 : g3xzh/rust/sum_bugfix, r=huonw
[rust.git] / src / test / bench / shootout-fasta.rs
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.
4 //
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.
10
11 /* -*- mode: rust; indent-tabs-mode: nil -*-
12  * Implementation of 'fasta' benchmark from
13  * Computer Language Benchmarks Game
14  * http://shootout.alioth.debian.org/
15  */
16
17 use std::io;
18 use std::io::buffered::BufferedWriter;
19 use std::io::File;
20 use std::num::min;
21 use std::os;
22
23 static LINE_LENGTH: uint = 60;
24 static IM: u32 = 139968;
25
26 struct MyRandom {
27     last: u32
28 }
29 impl MyRandom {
30     fn new() -> MyRandom { MyRandom { last: 42 } }
31     fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
32     fn gen(&mut self) -> u32 {
33         self.last = (self.last * 3877 + 29573) % IM;
34         self.last
35     }
36 }
37
38 struct AAGen<'a> {
39     rng: &'a mut MyRandom,
40     data: ~[(u32, u8)]
41 }
42 impl<'a> AAGen<'a> {
43     fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
44         let mut cum = 0.;
45         let data = aa.iter()
46             .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
47             .collect();
48         AAGen { rng: rng, data: data }
49     }
50 }
51 impl<'a> Iterator<u8> for AAGen<'a> {
52     fn next(&mut self) -> Option<u8> {
53         let r = self.rng.gen();
54         self.data.iter()
55             .skip_while(|pc| pc.n0() < r)
56             .map(|&(_, c)| c)
57             .next()
58     }
59 }
60
61 fn make_fasta<W: Writer, I: Iterator<u8>>(
62     wr: &mut W, header: &str, mut it: I, mut n: uint)
63 {
64     wr.write(header.as_bytes());
65     let mut line = [0u8, .. LINE_LENGTH + 1];
66     while n > 0 {
67         let nb = min(LINE_LENGTH, n);
68         for i in range(0, nb) {
69             line[i] = it.next().unwrap();
70         }
71         n -= nb;
72         line[nb] = '\n' as u8;
73         wr.write(line.slice_to(nb + 1));
74     }
75 }
76
77 fn run<W: Writer>(writer: &mut W) {
78     let args = os::args();
79     let n = if os::getenv("RUST_BENCH").is_some() {
80         25000000
81     } else if args.len() <= 1u {
82         1000
83     } else {
84         from_str(args[1]).unwrap()
85     };
86
87     let rng = &mut MyRandom::new();
88     let alu =
89         "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
90         GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
91         CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
92         ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
93         GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
94         AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
95         AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
96     let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
97                 ('t', 0.27), ('B', 0.02), ('D', 0.02),
98                 ('H', 0.02), ('K', 0.02), ('M', 0.02),
99                 ('N', 0.02), ('R', 0.02), ('S', 0.02),
100                 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
101     let homosapiens = &[('a', 0.3029549426680),
102                         ('c', 0.1979883004921),
103                         ('g', 0.1975473066391),
104                         ('t', 0.3015094502008)];
105
106     make_fasta(writer, ">ONE Homo sapiens alu\n",
107                alu.as_bytes().iter().cycle().map(|c| *c), n * 2);
108     make_fasta(writer, ">TWO IUB ambiguity codes\n",
109                AAGen::new(rng, iub), n * 3);
110     make_fasta(writer, ">THREE Homo sapiens frequency\n",
111                AAGen::new(rng, homosapiens), n * 5);
112
113     writer.flush();
114 }
115
116 fn main() {
117     if os::getenv("RUST_BENCH").is_some() {
118         let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
119         run(&mut file);
120     } else {
121         run(&mut BufferedWriter::new(io::stdout()));
122     }
123 }