-
Notifications
You must be signed in to change notification settings - Fork 0
/
parallel.rs
55 lines (43 loc) · 1.26 KB
/
parallel.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use std::time::Instant;
use rayon::iter::{IntoParallelIterator, ParallelIterator};
use slacktor::{
actor::{Actor, Handler, Message},
Slacktor,
};
struct TestMessage(pub u64);
impl Message for TestMessage {
type Result = u64;
}
struct TestActor(pub u64);
impl Actor for TestActor {
fn destroy(&self) {
println!("destroying");
}
}
impl Handler<TestMessage> for TestActor {
fn handle_message(&self, m: TestMessage) -> u64 {
m.0 ^ self.0
}
}
fn main() {
// Create a slacktor instance
let mut system = Slacktor::new();
// Create a new actor
let actor_id = system.spawn(TestActor(rand::random::<u64>()));
// Get a reference to the actor
let a = system.get::<TestActor>(actor_id).unwrap();
// Time 1 billion messages, appending each to a vector and doing some math to prevent the
// code being completely optimzied away.
let num_messages = 1_000_000_000;
let start = Instant::now();
let _v = (0..num_messages).into_par_iter().map(|i| {
// Send the message
a.send(TestMessage(i as u64))
}).collect::<Vec<_>>();
let elapsed = start.elapsed();
println!(
"{:.2} messages/sec",
num_messages as f64 / elapsed.as_secs_f64()
);
system.kill(actor_id);
}