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