@@ -25,7 +25,7 @@ pub(crate) struct NamedBench<'a, I, O> {
2525 pub fun : CallBench < ' a , I , O > ,
2626 pub num_group_iter : usize ,
2727}
28- impl < ' a , I , O > NamedBench < ' a , I , O > {
28+ impl < ' a , I , O : OutputValue > NamedBench < ' a , I , O > {
2929 pub fn new ( bench_id : BenchId , fun : CallBench < ' a , I , O > , num_group_iter : usize ) -> Self {
3030 Self {
3131 bench_id,
@@ -174,7 +174,7 @@ impl<O> RunResult<O> {
174174 }
175175}
176176
177- impl < ' a , I , O > NamedBench < ' a , I , O > {
177+ impl < ' a , I , O : OutputValue > NamedBench < ' a , I , O > {
178178 #[ inline]
179179 /// Each group has its own number of iterations. This is not the final num_iter
180180 pub fn sample_and_get_iter ( & mut self , input : & ' a I ) -> usize {
@@ -216,14 +216,27 @@ impl<'a, I, O> NamedBench<'a, I, O> {
216216 plugins. emit ( PluginEvents :: BenchStart {
217217 bench_id : & self . bench_id ,
218218 } ) ;
219+ debug_assert ! ( num_iter > 0 ) ;
219220 let start = std:: time:: Instant :: now ( ) ;
220- let mut res: Option < O > = None ;
221- for _ in 0 ..num_iter {
222- res = Some ( black_box ( ( self . fun ) ( input) ) ) ;
223- }
224- let elapsed = start. elapsed ( ) ;
225221
226- let run_result = RunResult :: new ( elapsed. as_nanos ( ) as u64 / num_iter as u64 , res. unwrap ( ) ) ;
222+ // Defer dropping outputs so destructor cost is not part of the measured time.
223+ let run_result = if O :: defer_drop ( ) {
224+ let mut outputs: Vec < O > = Vec :: with_capacity ( num_iter) ;
225+ for _ in 0 ..num_iter {
226+ outputs. push ( black_box ( ( self . fun ) ( input) ) ) ;
227+ }
228+ let duration_ns = start. elapsed ( ) . as_nanos ( ) as u64 / num_iter as u64 ;
229+ let last_output = outputs. pop ( ) . expect ( "num_iter > 0" ) ;
230+ RunResult :: new ( duration_ns, last_output)
231+ } else {
232+ let mut res: Option < O > = None ;
233+ for _ in 0 ..num_iter {
234+ res = Some ( black_box ( ( self . fun ) ( input) ) ) ;
235+ }
236+ let duration_ns = start. elapsed ( ) . as_nanos ( ) as u64 / num_iter as u64 ;
237+ RunResult :: new ( duration_ns, res. unwrap ( ) )
238+ } ;
239+
227240 plugins. emit ( PluginEvents :: BenchStop {
228241 bench_id : & self . bench_id ,
229242 duration : run_result. duration_ns ,
0 commit comments