time_it.rs (1192B)
1 // Copyright 2022 Adobe. All rights reserved. 2 // This file is licensed to you under the Apache License, 3 // Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) 4 // or the MIT license (http://opensource.org/licenses/MIT), 5 // at your option. 6 7 // Unless required by applicable law or agreed to in writing, 8 // this software is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or 10 // implied. See the LICENSE-MIT and LICENSE-APACHE files for the 11 // specific language governing permissions and limitations under 12 // each license. 13 14 use std::time::Instant; 15 16 use tracing::info; 17 18 // (Internal debugging tool.) 19 // Measure and log the time from the creation of this struct until it is dropped. 20 pub(crate) struct TimeIt { 21 label: &'static str, 22 start: Instant, 23 } 24 25 // Justification for dead_code: This is a debugging tool that is not always needed. 26 #[allow(dead_code)] 27 impl TimeIt { 28 pub fn new(label: &'static str) -> Self { 29 Self { 30 label, 31 start: Instant::now(), 32 } 33 } 34 } 35 impl Drop for TimeIt { 36 fn drop(&mut self) { 37 info!("timing for {}: {:.2?}", self.label, self.start.elapsed()); 38 } 39 }