]> git.lizzy.rs Git - rust.git/blob - src/test/bench/shootout-regex-dna.rs
rollup merge of #17355 : gamazeps/issue17210
[rust.git] / src / test / bench / shootout-regex-dna.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) 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 // ignore-stage1
42 // ignore-cross-compile #12102
43
44 #![feature(macro_rules, phase)]
45
46 extern crate regex;
47 #[phase(plugin)]extern crate regex_macros;
48
49 use std::io;
50 use regex::{NoExpand, Regex};
51 use std::sync::{Arc, Future};
52
53 fn count_matches(seq: &str, variant: &Regex) -> int {
54     let mut n = 0;
55     for _ in variant.find_iter(seq) {
56         n += 1;
57     }
58     n
59 }
60
61 fn main() {
62     let mut rdr = if std::os::getenv("RUST_BENCH").is_some() {
63         let fd = io::File::open(&Path::new("shootout-k-nucleotide.data"));
64         box io::BufferedReader::new(fd) as Box<io::Reader>
65     } else {
66         box io::stdin() as Box<io::Reader>
67     };
68     let mut seq = rdr.read_to_string().unwrap();
69     let ilen = seq.len();
70
71     seq = regex!(">[^\n]*\n|\n").replace_all(seq.as_slice(), NoExpand(""));
72     let seq_arc = Arc::new(seq.clone()); // copy before it moves
73     let clen = seq.len();
74
75     let mut seqlen = Future::spawn(proc() {
76         let substs = vec![
77             (regex!("B"), "(c|g|t)"),
78             (regex!("D"), "(a|g|t)"),
79             (regex!("H"), "(a|c|t)"),
80             (regex!("K"), "(g|t)"),
81             (regex!("M"), "(a|c)"),
82             (regex!("N"), "(a|c|g|t)"),
83             (regex!("R"), "(a|g)"),
84             (regex!("S"), "(c|g)"),
85             (regex!("V"), "(a|c|g)"),
86             (regex!("W"), "(a|t)"),
87             (regex!("Y"), "(c|t)"),
88         ];
89         let mut seq = seq;
90         for (re, replacement) in substs.into_iter() {
91             seq = re.replace_all(seq.as_slice(), NoExpand(replacement));
92         }
93         seq.len()
94     });
95
96     let variants = vec![
97         regex!("agggtaaa|tttaccct"),
98         regex!("[cgt]gggtaaa|tttaccc[acg]"),
99         regex!("a[act]ggtaaa|tttacc[agt]t"),
100         regex!("ag[act]gtaaa|tttac[agt]ct"),
101         regex!("agg[act]taaa|ttta[agt]cct"),
102         regex!("aggg[acg]aaa|ttt[cgt]ccct"),
103         regex!("agggt[cgt]aa|tt[acg]accct"),
104         regex!("agggta[cgt]a|t[acg]taccct"),
105         regex!("agggtaa[cgt]|[acg]ttaccct"),
106     ];
107     let (mut variant_strs, mut counts) = (vec!(), vec!());
108     for variant in variants.into_iter() {
109         let seq_arc_copy = seq_arc.clone();
110         variant_strs.push(variant.to_string());
111         counts.push(Future::spawn(proc() {
112             count_matches(seq_arc_copy.as_slice(), &variant)
113         }));
114     }
115
116     for (i, variant) in variant_strs.iter().enumerate() {
117         println!("{} {}", variant, counts.get_mut(i).get());
118     }
119     println!("");
120     println!("{}", ilen);
121     println!("{}", clen);
122     println!("{}", seqlen.get());
123 }