Skip to content

Commit a6cfc78

Browse files
committed
use criterion for benchmarks, tidy the code
1 parent 47de6e3 commit a6cfc78

File tree

10 files changed

+1742
-1652
lines changed

10 files changed

+1742
-1652
lines changed

Cargo.toml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "lazy-static-include"
3-
version = "1.2.0"
3+
version = "1.2.1"
44
authors = ["Magic Len <len@magiclen.org>"]
55
repository = "https://github.com/magiclen/lazy-static-include"
66
homepage = "https://magiclen.org/lazy-static-include"
@@ -17,5 +17,10 @@ appveyor = { repository = "magiclen/lazy-static-include", branch = "master", ser
1717
[dependencies]
1818
lazy_static = "1.1"
1919

20-
[features]
21-
benchmark = []
20+
[dev-dependencies]
21+
criterion = "0.2.5"
22+
serde_json = "1.0.33"
23+
24+
[[bench]]
25+
name = "bench"
26+
harness = false

README.md

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ assert_eq!(-3, TEST[1][2]);
102102
assert_eq!(-4, TEST[1][3]);
103103
assert_eq!(-5, TEST[1][4]);
104104
```
105+
105106
```rust
106107
#[macro_use] extern crate lazy_static_include;
107108
#[macro_use] extern crate lazy_static;
@@ -115,24 +116,39 @@ assert_eq!("哈囉", TEST[2]);
115116

116117
## Benchmark
117118

118-
Using static mechanisms makes your program faster. See my benchmark result below (Intel i7-6700HQ, ran on 2018/09/10):
119+
Using static mechanisms makes your program faster. See my benchmark result below (Intel i7-6700HQ, ran on 2018/11/14):
119120

120121
```text
121-
running 9 tests
122-
test include_array_lazy_static ... bench: 44 ns/iter (+/- 2)
123-
test include_array_native_static ... bench: 44 ns/iter (+/- 2)
124-
test include_array_no_static ... bench: 8,470 ns/iter (+/- 568)
125-
test include_bytes_lazy_static ... bench: 473 ns/iter (+/- 84)
126-
test include_bytes_native_static ... bench: 482 ns/iter (+/- 30)
127-
test include_bytes_no_static ... bench: 7,247 ns/iter (+/- 1,183)
128-
test include_str_lazy_static ... bench: 963 ns/iter (+/- 85)
129-
test include_str_native_static ... bench: 970 ns/iter (+/- 76)
130-
test include_str_no_static ... bench: 8,338 ns/iter (+/- 556)
122+
include_str/include_str_no_static
123+
time: [8.3773 us 8.4061 us 8.4361 us]
124+
include_str/include_str_native_static
125+
time: [965.65 ns 969.47 ns 973.04 ns]
126+
include_str/include_str_lazy_static
127+
time: [955.93 ns 958.78 ns 961.88 ns]
128+
129+
include_bytes/include_bytes_no_static
130+
time: [7.7806 us 7.8056 us 7.8318 us]
131+
include_bytes/include_bytes_native_static
132+
time: [418.43 ns 420.12 ns 421.83 ns]
133+
include_bytes/include_bytes_lazy_static
134+
time: [413.43 ns 415.14 ns 417.37 ns]
135+
136+
include_array/include_array_no_static
137+
time: [30.125 us 30.285 us 30.445 us]
138+
include_array/include_array_native_static
139+
time: [38.510 ns 38.640 ns 38.786 ns]
140+
include_array/include_array_lazy_static
141+
time: [39.713 ns 39.863 ns 40.019 ns]
131142
```
132143

133-
This benchmark program is in `tests/benchmark.rs`.
134144
When using the **release** profile, the performance of `lazy_static_include_*` is very close to `include_*`. That means you don't need to worry about the overhead, but just enjoy the faster compilation time.
135145

146+
You can run the benchmark program by executing,
147+
148+
```bash
149+
cargo bench
150+
```
151+
136152
## Crates.io
137153

138154
https://crates.io/crates/lazy-static-include

benches/bench.rs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
#[macro_use]
2+
extern crate criterion;
3+
4+
#[macro_use]
5+
extern crate lazy_static;
6+
7+
#[macro_use]
8+
extern crate lazy_static_include;
9+
10+
extern crate serde_json;
11+
12+
use std::fs::File;
13+
use std::io::Read;
14+
15+
use criterion::{Criterion, Benchmark};
16+
17+
fn include_str_no_static(c: &mut Criterion) {
18+
let path = concat!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
19+
20+
c.bench(
21+
"include_str",
22+
Benchmark::new("include_str_no_static", move |b| {
23+
b.iter(|| {
24+
let mut f = File::open(&path).unwrap();
25+
26+
let mut v = Vec::new();
27+
28+
f.read_to_end(&mut v).unwrap();
29+
30+
let s = String::from_utf8(v).unwrap();
31+
32+
assert!(s.contains("figarofigaro"));
33+
});
34+
}),
35+
);
36+
}
37+
38+
fn include_str_native_static(c: &mut Criterion) {
39+
let text = include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
40+
41+
c.bench(
42+
"include_str",
43+
Benchmark::new("include_str_native_static", move |b| {
44+
b.iter(|| {
45+
assert!(text.contains("figarofigaro"));
46+
});
47+
}),
48+
);
49+
}
50+
51+
fn include_str_lazy_static(c: &mut Criterion) {
52+
lazy_static_include_str!(TEXT, "data/benchmark.txt");
53+
54+
c.bench(
55+
"include_str",
56+
Benchmark::new("include_str_lazy_static", move |b| {
57+
b.iter(|| {
58+
assert!((*TEXT).contains("figarofigaro"));
59+
});
60+
}),
61+
);
62+
}
63+
64+
fn include_bytes_no_static(c: &mut Criterion) {
65+
let path = concat!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
66+
67+
c.bench(
68+
"include_bytes",
69+
Benchmark::new("include_bytes_no_static", move |b| {
70+
b.iter(|| {
71+
let mut f = File::open(&path).unwrap();
72+
73+
let mut v = Vec::new();
74+
75+
f.read_to_end(&mut v).unwrap();
76+
77+
String::from_utf8(v).unwrap();
78+
});
79+
}),
80+
);
81+
}
82+
83+
fn include_bytes_native_static(c: &mut Criterion) {
84+
let data = include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
85+
86+
c.bench(
87+
"include_bytes",
88+
Benchmark::new("include_bytes_native_static", move |b| {
89+
b.iter(|| {
90+
String::from_utf8(data.to_vec()).unwrap();
91+
});
92+
}),
93+
);
94+
}
95+
96+
fn include_bytes_lazy_static(c: &mut Criterion) {
97+
lazy_static_include_bytes!(DATA, "data/benchmark.txt");
98+
99+
c.bench(
100+
"include_bytes",
101+
Benchmark::new("include_bytes_lazy_static", move |b| {
102+
b.iter(|| {
103+
String::from_utf8((*DATA).to_vec()).unwrap();
104+
});
105+
}),
106+
);
107+
}
108+
109+
fn include_array_no_static(c: &mut Criterion) {
110+
let path = concat!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
111+
112+
c.bench(
113+
"include_array",
114+
Benchmark::new("include_array_no_static", move |b| {
115+
b.iter(|| {
116+
let mut f = File::open(&path).unwrap();
117+
118+
let mut v = Vec::new();
119+
120+
f.read_to_end(&mut v).unwrap();
121+
122+
let array: Vec<&str> = serde_json::from_slice(&v).unwrap();
123+
124+
assert!(array.binary_search(&"figarofigaro").is_ok());
125+
});
126+
}),
127+
);
128+
}
129+
130+
fn include_array_native_static(c: &mut Criterion) {
131+
let array = include!(concat!(env!("CARGO_MANIFEST_DIR"), "/", "data/benchmark.txt"));
132+
133+
c.bench(
134+
"include_array",
135+
Benchmark::new("include_array_native_static", move |b| {
136+
b.iter(|| {
137+
assert!(array.binary_search(&"figarofigaro").is_ok());
138+
});
139+
}),
140+
);
141+
}
142+
143+
fn include_array_lazy_static(c: &mut Criterion) {
144+
lazy_static_include_array!(ARRAY: [&'static str; 622], "data/benchmark.txt");
145+
146+
c.bench(
147+
"include_array",
148+
Benchmark::new("include_array_lazy_static", move |b| {
149+
b.iter(|| {
150+
assert!((*ARRAY).binary_search(&"figarofigaro").is_ok());
151+
});
152+
}),
153+
);
154+
}
155+
156+
criterion_group!(include_str, include_str_no_static, include_str_native_static, include_str_lazy_static);
157+
criterion_group!(include_bytes, include_bytes_no_static, include_bytes_native_static, include_bytes_lazy_static);
158+
criterion_group!(include_array, include_array_no_static, include_array_native_static, include_array_lazy_static);
159+
160+
criterion_main!(include_str, include_bytes, include_array);

0 commit comments

Comments
 (0)