Skip to content

Commit

Permalink
added conversion from string to uexp
Browse files Browse the repository at this point in the history
  • Loading branch information
LarWong committed Mar 10, 2024
1 parent 02d9590 commit db7ffdf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ test:

perf-test:
dune build @src/fmt --auto-promote src --profile dev
node $(SRC_DIR)/haz3lweb/perfTest.bc.js
node $(SRC_DIR)/haz3lweb/perfTest.bc.js "2 + 2"

clean:
dune clean
47 changes: 24 additions & 23 deletions src/haz3lweb/PerfTest.re
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
open Js_of_ocaml;
open Haz3lcore;
open Util;
open OptUtil.Syntax;
open Haz3lcore;

let mk_map = CoreSettings.on |> Interface.Statics.mk_map;
let ids = List.init(12, _ => Id.mk());
let id_at = x => x |> List.nth(ids);
let dhexp_of_uexp = u => Elaborator.dhexp_of_uexp(mk_map(u), u, false);

/* UExp of 1 + 1 */
let u: Term.UExp.t = {
ids: [id_at(0)],
term:
BinOp(
Int(Plus),
{ids: [id_at(1)], term: Int(1)},
{ids: [id_at(2)], term: Int(1)},
),
let dhexp_of_uexp = (u: Term.UExp.t) =>
Elaborator.dhexp_of_uexp(mk_map(u), u, false);
let uexp_of_string = (s: string) => {
let* z = Printer.zipper_of_string(s);
let u = Zipper.zip(z) |> MakeTerm.go |> fst;
Some(u);
};

/* Measures time from UExp to final DExp */
let measure_time_uexp = (u: Term.UExp.t) => {
print_endline(
"Measuring time to evaluate UExp \n\n\t" ++ Term.UExp.show(u) ++ "\n",
);
let measure_uexp_to_dhexp = (s: string) => {
print_endline("Measuring time to evaluate UExp of \n\n\t" ++ s ++ "\n");
let env = Builtins.env_init;
/* Create UExp before benchmarking time */
let* u = uexp_of_string(s);
let start_time = Js.Unsafe.global##.performance##now();
let* d = dhexp_of_uexp(u);
let (_, result) = Evaluator.evaluate(env, d);
Expand All @@ -40,8 +32,17 @@ let measure_time_uexp = (u: Term.UExp.t) => {
let print_time = t =>
print_endline("Elapsed Time: " ++ string_of_float(t) ++ " ms");

let time_str = measure_time_uexp(u);
switch (time_str) {
| Some(t) => print_time(t)
| None => print_endline("Cannot get elapsed time.")
let measure_time = (s: string) => {
switch (measure_uexp_to_dhexp(s)) {
| Some(t) => print_time(t)
| None => print_endline("Cannot get elapsed time.")
};
};

/* With CLI */
/* Js.Unsafe.js_expr("require('process')");
Js.Unsafe.js_expr("process.argv[2]") |> Js.to_string |> measure_time; */

/* Without CLI */
let sample_program = "1 + 1";
measure_time(sample_program);

0 comments on commit db7ffdf

Please sign in to comment.