performance measurements

Each table row shows performance measurements for this Rust program with a particular command-line input value N.

 N  CPU secs Elapsed secs Memory KB Code B ≈ CPU Load
60,0001.871.8733,6921669  3% 0% 1% 100%
600,00018.4418.4533,6921669  1% 0% 1% 100%
6,000,000185.36185.4233,6921669  1% 1% 1% 100%

Read the ↓ make, command line, and program output logs to see how this program was run.

Read chameneos-redux benchmark to see what this program should do.

 notes

rustc 1.0.0-alpha (44a287e6e 2015-01-08 17:03:40 -0800)

 chameneos-redux Rust program source code

// The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org/
//
// contributed by the Rust Project Developers
// contributed by TeXitoi

use self::Color::{Red, Yellow, Blue};
use std::sync::mpsc::{channel, Sender, Receiver};
use std::fmt;
use std::thread::Thread;

fn print_complements() {
    let all = [Blue, Red, Yellow];
    for aa in all.iter() {
        for bb in all.iter() {
            println!("{} + {} -> {}", *aa, *bb, transform(*aa, *bb));
        }
    }
}

enum Color {
    Red,
    Yellow,
    Blue,
}

impl Copy for Color {}

impl fmt::String for Color {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let str = match *self {
            Red => "red",
            Yellow => "yellow",
            Blue => "blue",
        };
        write!(f, "{}", str)
    }
}

struct CreatureInfo {
    name: usize,
    color: Color
}

impl Copy for CreatureInfo {}

fn show_color_list(set: Vec<Color>) -> String {
    let mut out = String::new();
    for col in set.iter() {
        out.push(' ');
        out.push_str(&format!("{}", col)[]);
    }
    out
}

fn show_digit(nn: usize) -> &'static str {
    match nn {
        0 => {" zero"}
        1 => {" one"}
        2 => {" two"}
        3 => {" three"}
        4 => {" four"}
        5 => {" five"}
        6 => {" six"}
        7 => {" seven"}
        8 => {" eight"}
        9 => {" nine"}
        _ => {panic!("expected digits from 0 to 9...")}
    }
}

struct Number(usize);
impl fmt::String for Number {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let mut out = vec![];
        let Number(mut num) = *self;
        if num == 0 { out.push(show_digit(0)) };

        while num != 0 {
            let dig = num % 10;
            num = num / 10;
            let s = show_digit(dig);
            out.push(s);
        }

        for s in out.iter().rev() {
            try!(write!(f, "{}", s))
        }
        Ok(())
    }
}

fn transform(aa: Color, bb: Color) -> Color {
    match (aa, bb) {
        (Red,    Red   ) => { Red    }
        (Red,    Yellow) => { Blue   }
        (Red,    Blue  ) => { Yellow }
        (Yellow, Red   ) => { Blue   }
        (Yellow, Yellow) => { Yellow }
        (Yellow, Blue  ) => { Red    }
        (Blue,   Red   ) => { Yellow }
        (Blue,   Yellow) => { Red    }
        (Blue,   Blue  ) => { Blue   }
    }
}

fn creature(
    name: usize,
    mut color: Color,
    from_rendezvous: Receiver<CreatureInfo>,
    to_rendezvous: Sender<CreatureInfo>,
    to_rendezvous_log: Sender<String>
) {
    let mut creatures_met = 0i32;
    let mut evil_clones_met = 0;
    let mut rendezvous = from_rendezvous.iter();

    loop {
        // ask for a pairing
        to_rendezvous.send(CreatureInfo {name: name, color: color}).unwrap();

        // log and change, or quit
        match rendezvous.next() {
            Some(other_creature) => {
                color = transform(color, other_creature.color);

                // track some statistics
                creatures_met += 1;
                if other_creature.name == name {
                   evil_clones_met += 1;
                }
            }
            None => break
        }
    }
    // log creatures met and evil clones of self
    let report = format!("{}{}", creatures_met, Number(evil_clones_met));
    to_rendezvous_log.send(report).unwrap();
}

fn rendezvous(nn: usize, set: Vec<Color>) {
    // these ports will allow us to hear from the creatures
    let (to_rendezvous, from_creatures) = channel::<CreatureInfo>();

    // these channels will be passed to the creatures so they can talk to us
    let (to_rendezvous_log, from_creatures_log) = channel::<String>();

    // these channels will allow us to talk to each creature by 'name'/index
    let to_creature: Vec<Sender<CreatureInfo>> =
        set.iter().enumerate().map(|(ii, &col)| {
            // create each creature as a listener with a port, and
            // give us a channel to talk to each
            let to_rendezvous = to_rendezvous.clone();
            let to_rendezvous_log = to_rendezvous_log.clone();
            let (to_creature, from_rendezvous) = channel();
            Thread::spawn(move|| {
                creature(ii,
                         col,
                         from_rendezvous,
                         to_rendezvous,
                         to_rendezvous_log);
            });
            to_creature
        }).collect();

    let mut creatures_met = 0;

    // set up meetings...
    for _ in (0..nn) {
        let fst_creature = from_creatures.recv().unwrap();
        let snd_creature = from_creatures.recv().unwrap();

        creatures_met += 2;

        to_creature[fst_creature.name].send(snd_creature).unwrap();
        to_creature[snd_creature.name].send(fst_creature).unwrap();
    }

    // tell each creature to stop
    drop(to_creature);

    // print each color in the set
    println!("{}", show_color_list(set));

    // print each creature's stats
    drop(to_rendezvous_log);
    for rep in from_creatures_log.iter() {
        println!("{}", rep);
    }

    // print the total number of creatures met
    println!("{}\n", Number(creatures_met));
}

fn main() {
    let nn = std::os::args().get(1).and_then(|arg| arg.parse()).unwrap_or(600);

    print_complements();
    println!("");

    rendezvous(nn, vec!(Blue, Red, Yellow));

    rendezvous(nn,
        vec!(Blue, Red, Yellow, Red, Yellow, Blue, Red, Yellow, Red, Blue));
}

 make, command-line, and program output logs

Sat, 10 Jan 2015 23:24:44 GMT

MAKE:
/usr/local/src/rust/bin/rustc -C opt-level=3 -C target-cpu=core2 -C lto chameneosredux.rs -o chameneosredux.rust_run
chameneosredux.rs:29:6: 29:17 warning: use of unstable item: I/O and core have yet to be reconciled
chameneosredux.rs:29 impl fmt::String for Color {
                          ^~~~~~~~~~~
note: this feature may not be used in the beta release channel
<std macros>:2:20: 2:66 warning: use of unstable item: reconciling core and I/O may alter this definition
<std macros>:2 ( & mut * $ dst ) . write_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
chameneosredux.rs:51:41: 51:43 warning: use of unstable item: API still in development
chameneosredux.rs:51         out.push_str(&format!("{}", col)[]);
                                                             ^~
note: this feature may not be used in the beta release channel
chameneosredux.rs:73:6: 73:17 warning: use of unstable item: I/O and core have yet to be reconciled
chameneosredux.rs:73 impl fmt::String for Number {
                          ^~~~~~~~~~~
note: this feature may not be used in the beta release channel
<std macros>:2:20: 2:66 warning: use of unstable item: reconciling core and I/O may alter this definition
<std macros>:2 ( & mut * $ dst ) . write_fmt ( format_args ! ( $ ( $ arg ) * ) ) )
                                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
chameneosredux.rs:156:13: 156:26 warning: use of unstable item: may change with specifics of new Send semantics
chameneosredux.rs:156             Thread::spawn(move|| {
                                  ^~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
chameneosredux.rs:196:14: 196:27 warning: use of unstable item
chameneosredux.rs:196     let nn = std::os::args().get(1).and_then(|arg| arg.parse()).unwrap_or(600);
                                   ^~~~~~~~~~~~~
note: this feature may not be used in the beta release channel
chameneosredux.rs:196:56: 196:63 warning: use of unstable item: this method was just created
chameneosredux.rs:196     let nn = std::os::args().get(1).and_then(|arg| arg.parse()).unwrap_or(600);
                                                                             ^~~~~~~
note: this feature may not be used in the beta release channel
rm chameneosredux.rs
14.52s to complete and log all make actions

COMMAND LINE:
./chameneosredux.rust_run 6000000

PROGRAM OUTPUT:
blue + blue -> blue
blue + red -> yellow
blue + yellow -> red
red + blue -> yellow
red + red -> red
red + yellow -> blue
yellow + blue -> red
yellow + red -> blue
yellow + yellow -> yellow

 blue red yellow
3999541 zero
4000174 zero
4000285 zero
 one two zero zero zero zero zero zero

 blue red yellow red yellow blue red yellow red blue
1199661 zero
1199768 zero
1199909 zero
1199858 zero
1199974 zero
1200089 zero
1200226 zero
1200186 zero
1200171 zero
1200158 zero
 one two zero zero zero zero zero zero

Revised BSD license

  Home   Conclusions   License   Play