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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/// Enables profiling for the annotated function.
///
/// Equivalent to putting [`prof!()`] at the start.
///
/// # Examples
/// ```rust
/// use profi::profile;
///
/// #[profile]
/// fn anotated() {
/// // ...
/// }
/// ```
pub use profile;
/// Enables printing out the profiling results when `main` exits.
///
/// Equivalent to writing [`print_on_exit!()`] at the start of the function.
///
/// # Example
/// ```rust
/// #[profi::main]
/// fn main() {
/// // ...
/// }
/// ```
pub use main;
/// Allows profiling the profiling methods
pub type Str = Cow;
/// Profiles the time it takes for the scope to end.
///
/// If you want to get an explicit guard, use [`prof_guard!`].
///
/// # Examples
/// ## Infer function's name
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn sleep() {
/// // Profile `sleep`
/// prof!();
/// std::thread::sleep(std::time::Duration::from_millis(200));
/// }
///
/// fn main() {
/// print_on_exit!();
/// sleep();
/// }
/// ```
///
/// ## Use provided name
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn wait(ms: u64) {
/// // Profile `sleep`
/// prof!("wait millis");
/// std::thread::sleep(std::time::Duration::from_millis(ms));
/// }
///
/// fn main() {
/// print_on_exit!();
/// wait(15);
/// }
/// ```
///
/// ## Use dynamic name
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn main() {
/// print_on_exit!();
///
/// for i in 0..10 {
/// prof!(fmt = "iteration {i}");
/// }
/// }
/// ```
///
/// Returns a guard that will profile as long as it's alive.
///
/// This will be until the scope ends or is dropped manually.
///
/// Supports the same syntax as [`prof!`];
///
/// # Examples
/// ```
/// use profi::{prof_guard, print_on_exit};
///
/// fn sleep(time: u64) {
/// // Must be saved into an explicit guard, or it will be dropped at the end of the `if` block
/// let _guard = if time < 100 {
/// prof_guard!("< 100")
/// } else {
/// prof_guard!(">= 100")
/// };
/// std::thread::sleep(std::time::Duration::from_millis(time));
/// }
///
/// fn main() {
/// print_on_exit!();
///
/// sleep(50);
/// sleep(150);
/// }
/// ```
/// Prints the profiled timings to stdout when `main` exits.
///
/// Creates an implicit `main` profiling guard, which will profile the whole program's time.
///
/// **Always put at the top of the `main` function to ensure it's dropped last.**
///
/// Print to stderr instead with `print_on_exit!(stderr)`.
///
/// Or print to a `std::io::Write` with `print_on_exit!(to = std::io::stdout())`
///
/// # Examples
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn main() {
/// print_on_exit!();
/// // ...
/// }
/// ```
///
/// Print to stderr instead of stdout:
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn main() {
/// print_on_exit!(stderr);
/// // ...
/// }
/// ```
///
/// Print to a file:
/// ```
/// use profi::{prof, print_on_exit};
///
/// fn main() {
/// let mut file = Vec::<u8>::new();
/// print_on_exit!(to = &mut file, ondrop = |f| println!("{f:?}"));
/// // ...
/// }
/// ```