Every now and then you just need to compare a dozen almost-identical iterations of the solution to a basic toy programming problem to figure out what's faster or slower in your language of choice.
[WIP]
- The Leetcode problem
- Racing methodology
spork/test
'stimeit
function- Two tests: one that should return true and one that should return false
- My naive approach to solving this problem (
label
+return
)- Explanation
- Advantages
- Other approaches
- Use a variable
- Doesn't return early
- Use a
loop
- Not easy to reason about
- Use recursion
- A terrible idea in Janet!
- Gradual optimization
- Use a variable
- Permutations
- Using
array/slice
to avoid redundant calculations- Explanation
- Assessment
- Offset by the cost of calling
array/slice
!
- Offset by the cost of calling
- Using
zero?
vs= 0
- Explanation
- Assessment
zero?
is objectively slower by around a third (but sometimes the overall impact is closer to half!)- It is implemented in terms of
= 0
under the hood, but with a call tocompare
first
- Using
- Table of results and Analysis
- My naive solution is actually pretty performant
- Lessons learned
- Function calls are expensive in Janet
- Suspected reason for this: C instantiates a new object and allocates memory for every function call
= 0
is much more efficient thanzero?
- Recursion is not well-optimized in Janet
- Function calls are expensive in Janet