< 返回版块

2019-05-06 09:27    责任编辑:jay

标签:Novice,Learning

Standard Library Collections

执行时间

/* It prints all the integer numbers from 0 to 9999,
followed by a fractional number.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    let time0 = Instant::now();
    for i in 0..10_000 {
        println!("{}", i);
    }
    let time1 = Instant::now();
    println!("{}", elapsed_ms(time0, time1));
}
/* After some time of computation,
it prints three fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 100_000_000;
    let t0 = Instant::now();
    let mut v = Vec::<usize>::with_capacity(SIZE);
    let t1 = Instant::now();
    for i in 0..SIZE {
        v.push(i);
    }
    let t2 = Instant::now();
    for _ in 0..SIZE {
        v.pop();
    }
    let t3 = Instant::now();
    print!("{} {} {}", elapsed_ms(t0, t1),
        elapsed_ms(t1, t2), elapsed_ms(t2, t3));
}
/* After some time of computation,
it prints three fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 100_000;
    let t0 = Instant::now();
    let mut v = Vec::<usize>::with_capacity(SIZE);
    let t1 = Instant::now();
    for i in 0..SIZE {
        v.insert(0, i);
    }
    let t2 = Instant::now();
    for _ in 0..SIZE {
        v.remove(0);
    }
    let t3 = Instant::now();
    print!("{} {} {}", elapsed_ms(t0, t1),
        elapsed_ms(t1, t2), elapsed_ms(t2, t3));
}

队列

/* After some time of computation,
it prints two fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 40_000;
    let t0 = Instant::now();
    let mut v = Vec::<usize>::new();
    for i in 0..SIZE {
        v.push(i);
        v.push(SIZE + i);
        v.remove(0);
        v.push(SIZE * 2 + i);
        v.remove(0);
    }
    let t1 = Instant::now();
    while v.len() > 0 {
        v.remove(0);
    }
    let t2 = Instant::now();
    print!("{} {}", elapsed_ms(t0, t1), elapsed_ms(t1, t2));
}
/* After some time of computation,
it prints two fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 40_000;
    let t0 = Instant::now();
    let mut v = Vec::<usize>::new();
    for i in 0..SIZE {
        v.insert(0, i);
        v.insert(0, SIZE + i);
        v.pop();
        v.insert(0, SIZE * 2 + i);
        v.pop();
    }
    let t1 = Instant::now();
    while v.len() > 0 {
        v.pop();
    }
    let t2 = Instant::now();
    print!("{} {}", elapsed_ms(t0, t1), elapsed_ms(t1, t2));
}
/* After some time of computation,
it prints two fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 40_000;
    let t0 = Instant::now();
    let mut vd = std::collections::VecDeque::<usize>::new();
    for i in 0..SIZE {
        vd.push_back(i);
        vd.push_back(SIZE + i);
        vd.pop_front();
        vd.push_back(SIZE * 2 + i);
        vd.pop_front();
    }
    let t1 = Instant::now();
    while vd.len() > 0 {
        vd.pop_front();
    }
    let t2 = Instant::now();
    print!("{} {}", elapsed_ms(t0, t1), elapsed_ms(t1, t2));
}
/* After some time of computation,
it prints five fractional numbers.*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: usize = 40_000;
    let mut v = Vec::<usize>::new();
    let mut vd = std::collections::VecDeque::<usize>::new();
    let t0 = Instant::now();
    for i in 0..SIZE {
        v.push(i);
    }
    let t1 = Instant::now();
    for i in 0..SIZE {
        vd.push_back(i);
    }
    let mut count = 0;
    let t2 = Instant::now();
    for i in v.iter() {
        count += i;
    }
    let t3 = Instant::now();
    for i in vd.iter() {
        count += i;
    }
    let t4 = Instant::now();
    print!("{} {} {} {} {}", count,
        elapsed_ms(t0, t1), elapsed_ms(t1, t2),
        elapsed_ms(t2, t3), elapsed_ms(t3, t4));
}

二叉堆

/* It prints:
48 35 20 39 42 33 50 29 27 18 17 16 13 12 */
fn main() {
    fn add(v: &mut Vec<i32>, a: i32) {
        v.push(a);
        v.sort();
    }
    let a = [48, 18, 20, 35, 17, 13, 39,
        12, 42, 33, 29, 27, 50, 16];
    let mut v = Vec::<i32>::new();
    for i in 0..a.len() / 2 {
        add(&mut v, a[i * 2]);
        add(&mut v, a[i * 2 + 1]);
        print!("{} ", v.pop().unwrap());
    }
    while ! v.is_empty() {
        print!("{} ", v.pop().unwrap());
    }
}
/* It prints:
48 35 20 39 42 33 50 29 27 18 17 16 13 12 */
fn main() {
    fn extract(v: &mut Vec<i32>) -> Option<i32> {
        v.sort();
        v.pop()
    }
    let a = [48, 18, 20, 35, 17, 13, 39,
        12, 42, 33, 29, 27, 50, 16];
    let mut v = Vec::<i32>::new();
    for i in 0..a.len() / 2 {
        v.push(a[i * 2]);
        v.push(a[i * 2 + 1]);
        print!("{} ", extract(&mut v).unwrap());
    }
    while ! v.is_empty() {
        print!("{} ", extract(&mut v).unwrap());
    }
}
/* It prints:
48 35 20 39 42 33 50 29 27 18 17 16 13 12 */
fn main() {
    let a = [48, 18, 20, 35, 17, 13, 39,
        12, 42, 33, 29, 27, 50, 16];
    let mut v = std::collections::BinaryHeap::<i32>::new();
    for i in 0..a.len() / 2 {
        v.push(a[i * 2]);
        v.push(a[i * 2 + 1]);
        print!("{} ", v.pop().unwrap());
    }
    while ! v.is_empty() {
        print!("{} ", v.pop().unwrap());
    }
}

Ordered Sets and Unordered Sets

集合
11.

/* It prints:
Vec: 6 8 2 8 4 9 6 1 8 0. [6, 8, 2, 8, 4, 9, 6, 1, 8, 0]
HashSet : 6 1 9 2 0 8 4. {6, 1, 9, 2, 0, 8, 4}
BTreeSet: 0 1 2 4 6 8 9. {0, 1, 2, 4, 6, 8, 9}
*/
fn main() {
    let arr = [6, 8, 2, 8, 4, 9, 6, 1, 8, 0];
    let mut v = Vec::<_>::new();
    let mut hs = std::collections::HashSet::<_>::new();
    let mut bs = std::collections::BTreeSet::<_>::new();
    for i in arr.iter() {
        v.push(i);
        hs.insert(i);
        bs.insert(i);
    }
    print!("Vec:");
    for i in v.iter() { print!(" {}", i); }
    println!(". {:?}", v);
    print!("HashSet :");
    for i in hs.iter() { print!(" {}", i); }
    println!(". {:?}", hs);
    print!("BTreeSet:");
    for i in bs.iter() { print!(" {}", i); }
    println!(". {:?}", bs);
}
/* After some time of computation, it prints:
Pushes in Vec: <fractional number>
Insertions in HashSet: <fractional number>
Insertions in BTreeSet: <fractional number>
Linear search in Vec: <fractional number>
Sort of Vec: <fractional number>
Binary search in Vec: <fractional number>
Search in HashSet: <fractional number>
Search in BTreeSet: <fractional number>
*/
fn main() {
    use std::time::Instant;
    fn elapsed_ms(t1: Instant, t2: Instant) -> f64 {
        let t = t2 - t1;
        t.as_secs() as f64 * 1000.
            + t.subsec_nanos() as f64 / 1e6
    }

    const SIZE: i32 = 40_000;
    fn ns_per_op(t1: Instant, t2: Instant) -> f64 {
        elapsed_ms(t1, t2) / SIZE as f64 * 1_000_000.
    }
    let mut v = Vec::<_>::new();
    let mut hs = std::collections::HashSet::<_>::new();
    let mut bs = std::collections::BTreeSet::<_>::new();
    let t0 = Instant::now();
    for i in 0..SIZE { v.push(i); }
    let t1 = Instant::now();
    for i in 0..SIZE { hs.insert(i); }
    let t2 = Instant::now();
    for i in 0..SIZE { bs.insert(i); }
    let t3 = Instant::now();
    for i in 0..SIZE { if ! v.contains(&i) { return; } }
    let t4 = Instant::now();
    v.swap(10_000, 20_000);
    v.sort();
    let t5 = Instant::now();
    for i in 0..SIZE {
        if v.binary_search(&i).is_err() { return; }
    }
    let t6 = Instant::now();
    for i in 0..SIZE { if ! hs.contains(&i) { return; } }
    let t7 = Instant::now();
    for i in 0..SIZE { if ! bs.contains(&i) { return; } }
    let t8 = Instant::now();
    println!("Pushes in Vec: {}", ns_per_op(t0, t1));
    println!("Insertions in HashSet: {}", ns_per_op(t1, t2));
    println!("Insertions in BTreeSet: {}", ns_per_op(t2, t3));
    println!("Linear search in Vec: {}", ns_per_op(t3, t4));
    println!("Sort of Vec: {}", ns_per_op(t4, t5));
    println!("Binary search in Vec: {}", ns_per_op(t5, t6));
    println!("Search in HashSet: {}", ns_per_op(t6, t7));
    println!("Search in BTreeSet: {}", ns_per_op(t7, t8));
}

Ordered Dictionaries and Unordered Dictionaries

字典
13.

/* It prints:
Vec: 640: T, 917: C, 412: S, 670: T, 917: L,
    [(640, 'T'), (917, 'C'), (412, 'S'), (670, 'T'), (917, 'L')]
HashMap: 670: T, 412: S, 917: L, 640: T,
    {670: 'T', 412: 'S', 917: 'L', 640: 'T'}
BTreeMap: 412: S, 640: T, 670: T, 917: L,
    {412: 'S', 640: 'T', 670: 'T', 917: 'L'}
*/
fn main() {
    let arr = [(640, 'T'), (917, 'C'),
        (412, 'S'), (670, 'T'), (917, 'L')];
    let mut v = Vec::<_>::new();
    let mut hs = std::collections::HashMap::<_, _>::new();
    let mut bs = std::collections::BTreeMap::<_, _>::new();
    for &(key, value) in arr.iter() {
        v.push((key, value));
        hs.insert(key, value);
        bs.insert(key, value);
    }
    print!("Vec:");
    for &(key, value) in v.iter() {
        print!(" {}: {},", key, value);
    }
    println!("\n    {:?}", v);
    print!("HashMap:");
    for (key, value) in hs.iter() {
        print!(" {}: {},", key, value);
    }
    println!("\n    {:?}", hs);
    print!("BTreeMap:");
    for (key, value) in bs.iter() {
        print!(" {}: {},", key, value);
    }
    println!("\n    {:?}", bs);
}