Skip to content

Commit

Permalink
don't count item parsing time
Browse files Browse the repository at this point in the history
  • Loading branch information
corwinkuiper committed Aug 26, 2024
1 parent 597285a commit 00243b8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
5 changes: 4 additions & 1 deletion wasm/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ <h1>Dofus optimiser wasm benchmark</h1>
<p>Energy: <span id="benchmark_energy"></span></p>

<script type="module">
import init, { benchmark } from "./pkg/wasm.js";
import init, { benchmark, setup } from "./pkg/wasm.js";

async function main() {
await init();

// parse items
setup();

const t0 = performance.now();
const energy = benchmark();
const t1 = performance.now();
Expand Down
8 changes: 3 additions & 5 deletions wasm/src/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use dofus_set::items::ItemIndex;
use dofus_set::items::Items;
use dofus_set::stats::Stat;

pub fn bench() -> f64 {
let items = Items::new();

pub fn bench(items: &Items) -> f64 {
let mut weights = [0.0; 51];
weights[Stat::Power as usize] = 1.0;
weights[Stat::Strength as usize] = 1.0;
Expand All @@ -29,10 +27,10 @@ pub fn bench() -> f64 {
let mut initial_set: [Option<_>; 16] = [None; 16];
initial_set[0] = Some(ItemIndex::new_from_id(2019));

let optimiser = Optimiser::new(&config, initial_set, &items).unwrap();
let optimiser = Optimiser::new(&config, initial_set, items).unwrap();

let final_state = optimiser.optimise().unwrap();
let sets = final_state.sets(&items);
let sets = final_state.sets(items);

-final_state.energy(&config, &sets)
}
15 changes: 14 additions & 1 deletion wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
use std::sync::OnceLock;

use dofus_set::items::Items;
use wasm_bindgen::prelude::*;

mod benchmark;

fn get_items() -> &'static Items {
static ITEMS: OnceLock<Items> = OnceLock::new();
ITEMS.get_or_init(Items::new)
}

#[wasm_bindgen]
pub fn setup() {
get_items();
}

#[wasm_bindgen]
pub fn benchmark() -> f64 {
benchmark::bench()
benchmark::bench(get_items())
}

0 comments on commit 00243b8

Please sign in to comment.