]> git.proxmox.com Git - rustc.git/blob - src/test/bench/shootout-fasta.rs
Imported Upstream version 1.0.0~beta
[rustc.git] / src / test / bench / shootout-fasta.rs
1 // The Computer Language Benchmarks Game
2 // http://benchmarksgame.alioth.debian.org/
3 //
4 // contributed by the Rust Project Developers
5
6 // Copyright (c) 2012-2014 The Rust Project Developers
7 //
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions
12 // are met:
13 //
14 // - Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // - Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in
19 // the documentation and/or other materials provided with the
20 // distribution.
21 //
22 // - Neither the name of "The Computer Language Benchmarks Game" nor
23 // the name of "The Computer Language Shootout Benchmarks" nor the
24 // names of its contributors may be used to endorse or promote
25 // products derived from this software without specific prior
26 // written permission.
27 //
28 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
31 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
32 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
33 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 // OF THE POSSIBILITY OF SUCH DAMAGE.
40
41 #![feature(old_io, old_path, io, core)]
42
43 use std::cmp::min;
44 use std::old_io::*;
45 use std::old_io;
46 use std::old_path::Path;
47 use std::num::Float;
48 use std::env;
49
50 const LINE_LENGTH: usize = 60;
51 const IM: u32 = 139968;
52
53 struct MyRandom {
54 last: u32
55 }
56 impl MyRandom {
57 fn new() -> MyRandom { MyRandom { last: 42 } }
58 fn normalize(p: f32) -> u32 {(p * IM as f32).floor() as u32}
59 fn gen(&mut self) -> u32 {
60 self.last = (self.last * 3877 + 29573) % IM;
61 self.last
62 }
63 }
64
65 struct AAGen<'a> {
66 rng: &'a mut MyRandom,
67 data: Vec<(u32, u8)>
68 }
69 impl<'a> AAGen<'a> {
70 fn new<'b>(rng: &'b mut MyRandom, aa: &[(char, f32)]) -> AAGen<'b> {
71 let mut cum = 0.;
72 let data = aa.iter()
73 .map(|&(ch, p)| { cum += p; (MyRandom::normalize(cum), ch as u8) })
74 .collect();
75 AAGen { rng: rng, data: data }
76 }
77 }
78 impl<'a> Iterator for AAGen<'a> {
79 type Item = u8;
80
81 fn next(&mut self) -> Option<u8> {
82 let r = self.rng.gen();
83 self.data.iter()
84 .skip_while(|pc| pc.0 < r)
85 .map(|&(_, c)| c)
86 .next()
87 }
88 }
89
90 fn make_fasta<W: Writer, I: Iterator<Item=u8>>(
91 wr: &mut W, header: &str, mut it: I, mut n: usize)
92 -> std::old_io::IoResult<()>
93 {
94 try!(wr.write(header.as_bytes()));
95 let mut line = [0; LINE_LENGTH + 1];
96 while n > 0 {
97 let nb = min(LINE_LENGTH, n);
98 for i in 0..nb {
99 line[i] = it.next().unwrap();
100 }
101 n -= nb;
102 line[nb] = '\n' as u8;
103 try!(wr.write(&line[..nb+1]));
104 }
105 Ok(())
106 }
107
108 fn run<W: Writer>(writer: &mut W) -> std::old_io::IoResult<()> {
109 let mut args = env::args();
110 let n = if env::var_os("RUST_BENCH").is_some() {
111 25000000
112 } else if args.len() <= 1 {
113 1000
114 } else {
115 args.nth(1).unwrap().parse().unwrap()
116 };
117
118 let rng = &mut MyRandom::new();
119 let alu =
120 "GGCCGGGCGCGGTGGCTCACGCCTGTAATCCCAGCACTTTGG\
121 GAGGCCGAGGCGGGCGGATCACCTGAGGTCAGGAGTTCGAGA\
122 CCAGCCTGGCCAACATGGTGAAACCCCGTCTCTACTAAAAAT\
123 ACAAAAATTAGCCGGGCGTGGTGGCGCGCGCCTGTAATCCCA\
124 GCTACTCGGGAGGCTGAGGCAGGAGAATCGCTTGAACCCGGG\
125 AGGCGGAGGTTGCAGTGAGCCGAGATCGCGCCACTGCACTCC\
126 AGCCTGGGCGACAGAGCGAGACTCCGTCTCAAAAA";
127 let iub = &[('a', 0.27), ('c', 0.12), ('g', 0.12),
128 ('t', 0.27), ('B', 0.02), ('D', 0.02),
129 ('H', 0.02), ('K', 0.02), ('M', 0.02),
130 ('N', 0.02), ('R', 0.02), ('S', 0.02),
131 ('V', 0.02), ('W', 0.02), ('Y', 0.02)];
132 let homosapiens = &[('a', 0.3029549426680),
133 ('c', 0.1979883004921),
134 ('g', 0.1975473066391),
135 ('t', 0.3015094502008)];
136
137 try!(make_fasta(writer, ">ONE Homo sapiens alu\n",
138 alu.as_bytes().iter().cycle().cloned(), n * 2));
139 try!(make_fasta(writer, ">TWO IUB ambiguity codes\n",
140 AAGen::new(rng, iub), n * 3));
141 try!(make_fasta(writer, ">THREE Homo sapiens frequency\n",
142 AAGen::new(rng, homosapiens), n * 5));
143
144 writer.flush()
145 }
146
147 fn main() {
148 let res = if env::var_os("RUST_BENCH").is_some() {
149 let mut file = BufferedWriter::new(File::create(&Path::new("./shootout-fasta.data")));
150 run(&mut file)
151 } else {
152 run(&mut old_io::stdout())
153 };
154 res.unwrap()
155 }