Skip to content

Commit

Permalink
feat(knapsack): add benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcondiro committed Oct 25, 2023
1 parent 025710e commit c04dcd3
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions exercises/practice/knapsack/benches/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#![feature(test)]
extern crate knapsack;
extern crate test;

use knapsack::Item;
use std::cmp;
use std::hint::black_box;
use test::Bencher;

#[bench]
fn bench_fifteen(b: &mut Bencher) {
b.iter(|| knapsack::maximum_value(MAX_WEIGHT, black_box(&ITEMS)));
}
#[bench]
fn bench_fifteen_brute_force(b: &mut Bencher) {
b.iter(|| maximum_value(MAX_WEIGHT, black_box(&ITEMS)));
}

/// Simple and inefficient brute force. Can it be beat?
pub fn maximum_value(max_weight: u32, items: &[Item]) -> u32 {
if items.len() == 0 {
return 0;
}

// Put the item in the knapsack (if fits)
let mut value_taking = 0;
if items[0].weight <= max_weight {
value_taking = items[0].value + maximum_value(max_weight - items[0].weight, &items[1..]);
}

// Don't put the item in the knapsack
let value_not_taking = maximum_value(max_weight, &items[1..]);

// Return the value of the richest knapsack
cmp::max(value_taking, value_not_taking)
}

const MAX_WEIGHT: u32 = 750;
const ITEMS: [Item; 15] = [
Item {
weight: 70,
value: 135,
},
Item {
weight: 73,
value: 139,
},
Item {
weight: 77,
value: 149,
},
Item {
weight: 80,
value: 150,
},
Item {
weight: 82,
value: 156,
},
Item {
weight: 87,
value: 163,
},
Item {
weight: 90,
value: 173,
},
Item {
weight: 94,
value: 184,
},
Item {
weight: 98,
value: 192,
},
Item {
weight: 106,
value: 201,
},
Item {
weight: 110,
value: 210,
},
Item {
weight: 113,
value: 214,
},
Item {
weight: 115,
value: 221,
},
Item {
weight: 118,
value: 229,
},
Item {
weight: 120,
value: 240,
},
];

0 comments on commit c04dcd3

Please sign in to comment.