I went down a rabbit hole on this one and came up with a functional solution, but it was such a PITA I never ended up using it. It works tho so if anyone wants to try it out, here's some snippets and a general overview.
the general overview is:
- we make use of openscad's ability to read data from json files
- a background service regularly writes the current time to a json file, that we read and re-read multiple times
- a "time" function writes a log to the console of how long it's been since the prev time and the current time
- during the rendering of the model, before and after each thing we want to time, we call the time function
- as it's rendering, or after it's renered, just look at the console logs and you can see "this bit took 10ms", "that bit took 10s", "that bit took 5 mins" etc.
here's the util functions in my lib
// function get_current_time() = 0;
function get_current_time() = import("C:/tmp/time.json")["currentTime"];
module end_timer(start_time, note=false) {
note = note ? note : str("loaded ", parent_module(1));
elapsed_time = get_current_time() - start_time;
echo(str(elapsed_time, " tenths of a second. ", note));
}
module time(note="") {
start_time = get_current_time();
children();
end_timer(start_time, note);
}
/*
usage as parent module:
time("my note") my_module();
usage as pair of functions:
start_time = get_current_time();
// slow operations
end_timer(start_time, "my note");
*/
here's the time-writing service (typescript)
import { writeFileSync } from 'fs';
function getCurrentTime(): number {
return Math.floor(Date.now() / 100);
}
function writeTimeToJson() {
const currentTime = getCurrentTime();
const jsonData = JSON.stringify({ currentTime });
console.log("Writing", jsonData);
writeFileSync('C:/tmp/time.json', jsonData);
}
setInterval(writeTimeToJson, 100);
here's an example of time.json
{"currentTime":1737352349}
here's an example of how to use it in a project
module positive() {
difference() {
time("main body") cuboid3(size, xy_r=corner_r, bottom_r=bottom_r, top_r=wall_t/3, anchor=BOTTOM);
time("main basin") color("purple", 0.5) up(size.z+nill) cuboid3(basin_size, xy_r=corner_r-(wall_t/2), top_r=-wall_t/3, bottom_r=gap, anchor=TOP);
}
}
module negative() {
time("grid base") up(base_h) fwd(extra_back/2) back(extra_front/2) gridfinity_base_negative_grid(grid_size.x, grid_size.y, anchor=BOTTOM);
// up(base_h) gridfinity_base_negative_grid(grid_size.x, grid_size.y, anchor=BOTTOM, style="slim");
}
...