├── .gitignore ├── Cargo.toml ├── README.md ├── LICENSE └── src └── lib.rs /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled files 2 | *.o 3 | *.so 4 | *.rlib 5 | *.dll 6 | Cargo.lock 7 | 8 | # Executables 9 | *.exe 10 | 11 | # Generated by Cargo 12 | /target/ 13 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "timeit" 3 | version = "0.1.2" 4 | authors = ["Gustav Larsson "] 5 | description = "Timing macros for Rust modelled after Python's timeit" 6 | repository = "https://github.com/gustavla/timeit" 7 | readme = "README.md" 8 | keywords = ["timing", "time", "benchmark", "optimization", "performance"] 9 | license = "MIT" 10 | 11 | [lib] 12 | name = "timeit" 13 | doctest = false 14 | 15 | [dependencies] 16 | time = "0.1.26" 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Crates.io](https://img.shields.io/crates/v/timeit.svg)](https://crates.io/crates/timeit) 2 | 3 | # Timeit for Rust 4 | 5 | This crate provides macros that make it easy to benchmark blocks of code. It is 6 | inspired and named after [timeit](https://docs.python.org/3/library/timeit.html) from Python. 7 | 8 | ## Examples 9 | 10 | ```rust 11 | #[macro_use] 12 | extern crate timeit; 13 | 14 | fn main() { 15 | timeit!({ 16 | let mut x: Vec = Vec::new(); 17 | for i in 0..1000 { 18 | x.push(i); 19 | } 20 | }); 21 | } 22 | ``` 23 | 24 | This will output something like: 25 | 26 | ```text 27 | 10000 loops: 2.4843 µs 28 | ``` 29 | 30 | It will determine the number of loops automatically. To run a specified number of loops and 31 | save the elapsed time to a variable, use the `timeit_loops!` macro: 32 | 33 | ```rust 34 | let sec = timeit_loops!(100, { 35 | let mut x: Vec = Vec::new(); 36 | for i in 0..1000 { 37 | x.push(i); 38 | } 39 | }); 40 | ``` 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Gustav Larsson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! This crate provides macros that make it easy to benchmark blocks of code. It is inspired and 2 | //! named after timeit from Python. 3 | //! 4 | //! Example: 5 | //! 6 | //! ``` 7 | //! #[macro_use] 8 | //! extern crate timeit; 9 | //! 10 | //! fn main() { 11 | //! timeit!({ 12 | //! let mut x: Vec = Vec::new(); 13 | //! for i in 0..1000 { 14 | //! x.push(i); 15 | //! } 16 | //! }); 17 | //! } 18 | //! ``` 19 | //! 20 | //! This will output something like: 21 | //! 22 | //! ```text 23 | //! 10000 loops: 2.4843 µs 24 | //! ``` 25 | //! 26 | //! It will determine the number of loops automatically. To run a specified number of loops and 27 | //! save the elapsed time to a variable, use the `timeit_loops!` macro: 28 | //! 29 | //! ``` 30 | //! let sec = timeit_loops!(100, { 31 | //! let mut x: Vec = Vec::new(); 32 | //! for i in 0..1000 { 33 | //! x.push(i); 34 | //! } 35 | //! }); 36 | //! ``` 37 | extern crate time; 38 | 39 | use time::Timespec; 40 | 41 | /// A shortcut to time's `get_time` function. This is so that the user of timeit doesn't have to 42 | /// separately add a dependency for the time crate. 43 | pub fn get_time() -> Timespec { 44 | use time::get_time; 45 | get_time() 46 | } 47 | 48 | #[macro_export] 49 | /// Runs a block a specified number of times and returns the average time of execution. 50 | macro_rules! timeit_loops { 51 | ($loops:expr, $code:block) => ({ 52 | use timeit::get_time; 53 | 54 | let n = $loops; 55 | let start = get_time(); 56 | for _ in 0..n { 57 | $code 58 | } 59 | let end = get_time(); 60 | let sec = (end.sec - start.sec) as f64 + 61 | (end.nsec - start.nsec) as f64 / 1_000_000_000.0; 62 | 63 | sec / (n as f64) 64 | }) 65 | } 66 | 67 | #[macro_export] 68 | /// Runs a block several times and outputs the average time per loop. The number of loops is 69 | /// determined automatically. 70 | macro_rules! timeit { 71 | ($code:block) => ({ 72 | let mut n = 1; 73 | let mut sec = timeit_loops!(n, $code); 74 | let mut again = true; 75 | 76 | let l = sec.log10().ceil() as isize; 77 | 78 | if l < -5 { 79 | n = 1000_000; 80 | } else if l <= 0 { 81 | n = 10isize.pow((-l) as u32); 82 | } else { 83 | again = false; 84 | } 85 | 86 | if again { 87 | sec = timeit_loops!(n, $code); 88 | } 89 | 90 | let (mult, unit_str) = if sec > 1.0 { 91 | (1.0, "s") 92 | } else if sec > 0.001 { 93 | (0.001, "ms") 94 | } else if sec > 0.000_001 { 95 | (0.000_001, "µs") 96 | } else { 97 | (0.000_000_001, "ns") 98 | }; 99 | 100 | println!("{} loops: {} {}", n, sec / mult, unit_str); 101 | }) 102 | } 103 | --------------------------------------------------------------------------------