-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrc_vs_py.rs.tmp
168 lines (151 loc) · 4.46 KB
/
rc_vs_py.rs.tmp
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use anyhow::Result;
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion};
use hashbrown::HashMap;
use pyo3::prelude::*;
use serde_json::Value;
use std::{fs, path::Path, rc::Rc};
struct RawEnt {
id: String,
props: HashMap<String, Vec<String>>,
}
struct Ent {
id: Rc<String>,
props: Rc<HashMap<String, Rc<Vec<String>>>>,
}
struct Ent3 {
id: String,
props: HashMap<String, Vec<String>>,
}
// struct Ent2 {
// id: Py<PyString>,
// props: Py<HashMap<String, Py<Vec<PyString>>>>,
// }
fn rcdesign(records: &[RawEnt]) -> usize {
let mut ents = vec![];
for record in records {
let props: HashMap<String, Rc<Vec<String>>> = record
.props
.iter()
.map(|(k, v)| (k.to_owned(), Rc::new(v.to_owned())))
.collect();
let ent = Ent {
id: Rc::new(record.id.clone()),
props: Rc::new(props),
};
ents.push(ent);
}
let mut count = 0;
for ent in ents {
count += ent.id.len();
for (_, v) in ent.props.iter() {
count += v.len();
}
}
count
}
fn basedesign(records: &[RawEnt]) -> usize {
let mut ents = vec![];
for record in records {
let props: HashMap<String, Vec<String>> = record
.props
.iter()
.map(|(k, v)| (k.to_owned(), v.to_owned()))
.collect();
let ent = Ent3 {
id: record.id.clone(),
props: props,
};
ents.push(ent);
}
let mut count = 0;
for ent in ents {
count += ent.id.len();
for (_, v) in ent.props.iter() {
count += v.len();
}
}
count
}
fn pydesign(records: &[RawEnt]) -> usize {
let mut ents = vec![];
Python::with_gil(|py| {
for record in records {
let props: HashMap<String, Py<Vec<String>>> = record
.props
.iter()
.map(|(k, v)| (k.to_owned(), Py::new(py, v.to_owned()).unwrap()))
.collect();
let ent = Ent2 {
id: Py::new(py, record.id.clone()).unwrap(),
props: Py::new(py, props).unwrap(),
};
ents.push(ent);
}
});
let mut count = 0;
for ent in ents {
count += ent.id.len();
for (_, v) in ent.props.iter() {
count += v.len();
}
}
count
}
fn criterion_benchmark(c: &mut Criterion) {
let filename = "wikipedia/List_of_highest_mountains_on_Earth.html";
let datafile = Path::new(env!("CARGO_MANIFEST_DIR"))
.join("benches/resources")
.join("wdentities.jl");
let records: Vec<RawEnt> = fs::read_to_string(datafile)
.unwrap()
.split("\n")
.filter(|s| !s.is_empty())
.map(|r| {
let value: Value = serde_json::from_str(r).unwrap();
let id = value
.as_object()
.unwrap()
.get("id")
.unwrap()
.as_str()
.unwrap();
let props = value
.as_object()
.unwrap()
.get("props")
.unwrap()
.as_object()
.unwrap()
.into_iter()
.map(|(k, v)| {
let values: Vec<String> = v
.as_array()
.unwrap()
.into_iter()
.map(|v| {
v.as_object()
.unwrap()
.get("rank")
.unwrap()
.as_str()
.unwrap()
.to_owned()
})
.collect::<Vec<_>>();
(k.to_owned(), values)
})
.collect::<HashMap<String, Vec<String>>>();
RawEnt {
id: id.to_owned(),
props,
}
})
.collect();
let mut group = c.benchmark_group("RCvsPy");
group.measurement_time(std::time::Duration::from_secs(10));
group.bench_function("rc", |b| b.iter(|| rcdesign(&records)));
group.bench_function("base", |b| b.iter(|| basedesign(&records)));
// group.bench_function("py", |b| b.iter(|| pydesign(&records)));
}
criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);