Rust trial: write benchmark
This commit is contained in:
parent
6b0fad89dc
commit
0b3fe689cd
7 changed files with 345 additions and 0 deletions
86
lang-trials/rust-benchmark/src/main.rs
Normal file
86
lang-trials/rust-benchmark/src/main.rs
Normal file
|
@ -0,0 +1,86 @@
|
|||
use nalgebra::*;
|
||||
use std::f64::consts::PI as PI;
|
||||
use sycamore::{prelude::*, rt::{JsCast, JsValue}};
|
||||
use web_sys::{console, window};
|
||||
|
||||
mod engine;
|
||||
|
||||
fn main() {
|
||||
// set up a config option that forwards panic messages to `console.error`
|
||||
#[cfg(feature = "console_error_panic_hook")]
|
||||
console_error_panic_hook::set_once();
|
||||
|
||||
/*console::log_1(&"before test schur 60".into());*/
|
||||
/*let test_rand_mat = OMatrix::<f64, U60, U60>::identity();*/
|
||||
/*let test_rot_step = OMatrix::<f64, U56, U56>::identity();*/
|
||||
/*let test_schur = test_rand_mat.schur();
|
||||
console::log_1(&format!("after test schur").into());
|
||||
let test_eigvals = test_schur.complex_eigenvalues();
|
||||
console::log_1(&format!("after test eigenvalues").into());*/
|
||||
|
||||
sycamore::render(|| {
|
||||
let time_res: usize = 100;
|
||||
let time_step = create_signal(0.0);
|
||||
let run_time_report = create_signal(-1.0);
|
||||
let display = create_node_ref();
|
||||
|
||||
on_mount(move || {
|
||||
let performance = window().unwrap().performance().unwrap();
|
||||
let start_time = performance.now();
|
||||
let eigval_series = engine::rand_eigval_series::<U60>(time_res);
|
||||
let run_time = performance.now() - start_time;
|
||||
run_time_report.set(run_time);
|
||||
|
||||
let canvas = display
|
||||
.get::<DomNode>()
|
||||
.unchecked_into::<web_sys::HtmlCanvasElement>();
|
||||
let ctx = canvas
|
||||
.get_context("2d")
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.dyn_into::<web_sys::CanvasRenderingContext2d>()
|
||||
.unwrap();
|
||||
ctx.set_fill_style(&JsValue::from("white"));
|
||||
|
||||
create_effect(move || {
|
||||
// center and normalize the coordinate system
|
||||
let width = canvas.width() as f64;
|
||||
let height = canvas.height() as f64;
|
||||
ctx.set_transform(1.0, 0.0, 0.0, -1.0, 0.5*width, 0.5*height).unwrap();
|
||||
|
||||
// clear the previous frame
|
||||
ctx.clear_rect(-0.5*width, -0.5*width, width, height);
|
||||
|
||||
// find the resolution
|
||||
const R_DISP: f64 = 1.5;
|
||||
let res = width / (2.0*R_DISP);
|
||||
|
||||
// draw the eigenvalues
|
||||
let eigvals = &eigval_series[time_step.get() as usize];
|
||||
for n in 0..eigvals.len() {
|
||||
ctx.begin_path();
|
||||
ctx.arc(
|
||||
/* typecast only needed for single float version */
|
||||
res * f64::from(eigvals[n].re),
|
||||
res * f64::from(eigvals[n].im),
|
||||
3.0,
|
||||
0.0, 2.0*PI
|
||||
).unwrap();
|
||||
ctx.fill();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
view! {
|
||||
div(id="app") {
|
||||
div { (run_time_report.get()) " ms" }
|
||||
canvas(ref=display, width="600", height="600")
|
||||
input(
|
||||
type="range",
|
||||
max=(time_res - 1).to_string(),
|
||||
bind:valueAsNumber=time_step
|
||||
)
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue