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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//! # Arbitime
//!
//! A simple Rust library for timing code execution with convenient macros.
//!
//! ## Features
//!
//! - [`time!`] - Time code execution and return both duration and result
//! - [`format_time!`] - Time code execution and format duration as a string
//! - [`log_time!`] - Time code execution with automatic logging to stderr
//!
//! ## Examples
//!
//! ### Basic timing with [`time!`]
//!
//! ```rust
//! use arbitime::time;
//!
//! let (duration, result) = time! {
//! // Some expensive computation
//! (1..=1000000).sum::<u64>()
//! };
//!
//! println!("Result: {}, took: {:?}", result, duration);
//! ```
//!
//! ### Formatted timing with [`format_time!`]
//!
//! ```rust
//! use arbitime::format_time;
//!
//! let (msg, result) = format_time!("Computing sum" => {
//! (1..=1000).sum::<u32>()
//! });
//!
//! println!("{}", msg); // "Computing sum - Execution time: ..."
//! ```
//!
//! ### Logging timing with [`log_time!`]
//!
//! ```rust
//! use arbitime::log_time;
//!
//! // Single operation with custom message
//! let result = log_time!("Computing sum" => {
//! (1..=1000).sum::<u32>()
//! });
//!
//! // Simple timing without custom message
//! log_time! {
//! (1..=100).sum::<u32>()
//! };
//! ```
/// Times the execution of a code block and returns both the duration and result.
///
/// This macro measures the time it takes to execute the given code and returns
/// a tuple containing the duration and the result of the code execution.
///
/// # Examples
///
/// ```rust
/// use arbitime::time;
///
/// let (duration, result) = time! {
/// let mut sum = 0;
/// for i in 1..=100 {
/// sum += i;
/// }
/// sum
/// };
///
/// assert_eq!(result, 5050);
/// println!("Computation took: {:?}", duration);
/// ```
///
/// # Returns
///
/// A tuple `(Duration, T)` where:
/// - `Duration` is the time elapsed during execution
/// - `T` is the result of the executed code
/// Times the execution of code blocks and formats the duration as a string.
///
/// This macro provides several convenient ways to time code execution and format
/// the timing information as a string. It supports single operations, multiple
/// operations, and operations with custom messages.
///
/// # Examples
///
/// ## Single operation with message
///
/// ```rust
/// use arbitime::format_time;
///
/// let (msg, result) = format_time!(
/// "Database query" => {
/// // Simulate some work
/// 200
/// }
/// );
/// // msg contains: "Database query - Execution time: ..."
/// assert_eq!(result, 200);
/// ```
///
/// ## Multiple operations
///
/// ```rust
/// use arbitime::format_time;
///
/// let (msg1, result1) = format_time!("Fast calculation" => 2 + 2);
/// let (msg2, result2) = format_time!(
/// "Slow calculation" => {
/// let mut result = 0;
/// for i in 1..=100 {
/// result += i;
/// }
/// result
/// }
/// );
/// // Each call returns a formatted message and the result
/// ```
///
/// ## Simple timing without custom message
///
/// ```rust
/// use arbitime::format_time;
///
/// let (msg, result) = format_time! {
/// (1..=100).sum::<u32>()
/// };
/// // msg contains: "Execution time: ..."
/// ```
///
/// # Returns
///
/// A tuple `(String, T)` where:
/// - `String` is the formatted timing message
/// - `T` is the result of the executed code
);+
}
};
// Multiple message-body pairs without braces
=> ;
// Single message-body pair with braces
// This arm is redundant because the first macro arm already matches ($msg:expr => { $($body:tt)* })
// and handles the case. You can safely remove this arm.
// Single message-body pair without braces
=> ;
// Just body without message
=> ;
}
/// Times the execution of code and automatically logs the duration to stderr.
///
/// This is a convenience macro that combines [`format_time!`] with automatic logging.
/// It times the execution of code and prints the timing information to stderr,
/// returning only the result of the executed code.
///
/// # Examples
///
/// ## Single operation with message
///
/// ```rust
/// use arbitime::log_time;
///
/// let result = log_time!("Database query" => {
/// // Simulate some work
/// 42
/// });
/// // Prints: "Database query - Execution time: ..."
/// assert_eq!(result, 42);
/// ```
///
/// ## Simple timing without custom message
///
/// ```rust
/// use arbitime::log_time;
///
/// let result = log_time! {
/// (1..=100).sum::<u32>()
/// };
/// // Prints: "Execution time: ..."
/// ```
///
/// # Output
///
/// All timing information is printed to stderr using `eprintln!`.
///
/// # Returns
///
/// The result of the executed code (type `T`).