From 1c7a477dd5c9012da8df228d36731e3245045ce5 Mon Sep 17 00:00:00 2001 From: James Pace Date: Sun, 21 Jan 2024 09:53:02 -0500 Subject: [PATCH] powf is slow draft.: --- .../libm_vs_micromath_speed_comparison.qmd | 20 ++++++ blog/powf_is_slow.md | 61 +++++++++++++++++++ index.qmd | 7 ++- 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 _drafts/libm_vs_micromath_speed_comparison.qmd create mode 100644 blog/powf_is_slow.md diff --git a/_drafts/libm_vs_micromath_speed_comparison.qmd b/_drafts/libm_vs_micromath_speed_comparison.qmd new file mode 100644 index 0000000..100c982 --- /dev/null +++ b/_drafts/libm_vs_micromath_speed_comparison.qmd @@ -0,0 +1,20 @@ +--- +title: "Rust: libm vs micromath speed comparison" +author: "James Pace" +date: "2024/01/16" +--- + + diff --git a/blog/powf_is_slow.md b/blog/powf_is_slow.md new file mode 100644 index 0000000..c9baf83 --- /dev/null +++ b/blog/powf_is_slow.md @@ -0,0 +1,61 @@ +--- +title: "Rust: powf is slow" +author: "James Pace" +date: "2024/01/18" +--- + +I'm currently working on my motion planner on a microcontroller project and +was looking at switching from [libm][libm] to [micromath][micromath] as the +library I use for standard math operations. +For context, in Rust, normal math operations (like absolute value and square +root for floats) are provided as part of the standard library (called `std`). +The standard library is not available in environments that don't have a backing +operating system. +Crates than run in those environments thus can't use `std`, which is called being +`no_std`. +A lot of functions that are in `std` are actually in two other crates, `core` or `alloc` +which can be used in `no_std` environments. +The normal math functions aren't one of them, and therefore a different library has to be +used. + +My intitial implementation of the planner used `libm` (largely because I found it first), +and when I later found `micromath` I wanted to do some profiling comparisons to see which +one was faster. +I'm going to make a separate blog post with statistics from the profiling, but I wanted to +write a quick article today talking about `powf`. + +One of the operations my planner does a lot is find the distance between two things. +My initial implementation did something like +```rust +fn distance(point1: &Point, point2: &Point) -> f32 { + let squared_dist = powf((point2.x - point1.x), 2.0) + powf((point2.y - point1.y), 2.0); + sqrt(squared_dist) +} +``` +which works fine. + +When I was profiling I switched to: +```rust +fn distance(point1: &Point, point2: &Point) -> f32 { + let squared_dist = powf((point2.x - point1.x), 2.0) + powf((point2.y - point1.y), 2.0); + sqrt(squared_dist) +} +``` +which was noticeably faster for both `libm` and `micromath` particularly on the `microbit` +that I was using for profiling. + +## Does this replicate for `std::powf`? + +Not when building with `--release`. [^release-rant] + + + +## How much faster? + +[^release-rant]: As an aside, I think I'm going to switch to using `--release` when building rust code + all the time. The optimizations help a ton, to the extent I would never "use" software + not built with `--release` and extrapolating performance, even experientially, from + binaries built with the `dev` profile is just not transferrable at all. + +[libm]: https://github.com/rust-lang/libm +[micromath]: https://github.com/tarcieri/micromath diff --git a/index.qmd b/index.qmd index 75a9558..593380b 100644 --- a/index.qmd +++ b/index.qmd @@ -9,9 +9,14 @@ listing: sort: "date desc" --- -TIL focuses on things I learned "today" with an emphasis on robotics and +*Things I've Learned* or TIL is a site focused on things I've learned +and found interesting at some point in my life.[^1] + +A lot of the "things" will be centered around robotics, programming, and open source software. +[^1]: I know TIL normally stands for Today I Learned, but just focusing on what I learned +today isn't nearly as interesting. ## Latest Posts