rl_lang/interpreter/stdlib/debug/
bench.rs1use crate::{
2 interpreter::{
3 evaluator::Evaluator,
4 stdlib::common::{verr, vf, vok, vs},
5 values::Value,
6 },
7 utils::span::Span,
8};
9
10pub fn func(eval: &mut Evaluator, function: Value, iterations_val: Value) -> Value {
11 if !matches!(function, Value::Function { .. }) {
12 return verr!(vs!(format!(
13 "bench: expects a function or lambda, got {}",
14 function.type_name()
15 )));
16 }
17
18 let iterations = match iterations_val {
19 Value::Integer(n) if n > 0 => n as u64,
20 other => {
21 return verr!(vs!(format!(
22 "bench: expects a positive int for iterations, got {}",
23 other.type_name()
24 )));
25 }
26 };
27
28 let start = std::time::Instant::now();
29 for _ in 0..iterations {
30 if let Err(e) = eval.call_value(function.clone(), vec![], Span::dummy()) {
31 return verr!(vs!(format!(
32 "bench: error executing the function: {}",
33 e.message()
34 )));
35 };
36 }
37 let elapsed_ms = start.elapsed().as_secs_f64() * 1000.0;
38
39 vok!(vf!(elapsed_ms))
40}