by xondono · 5y ago · 0 comments
I found it odd, but I’ve seen weirder. Then I tried the simple example of writing a function to compute the nth fibonacci number (it’s almost the first example). After testing some numbers, I thought it seemed slow, so I quickly implemented the same function in Rust (with recursion too).
Rust took less time in compiling, and computing fib(n) for n 1..50 than Lisp to compute fib(50). Maybe I did something very wrong, but for now I’d rather learn Rust better.
In general, Common Lisp doesn't favor recursive functions (as opposed to Scheme), and as far as I know, most implementations don't put huge amounts of effort into optimizing it.
I'd be interested to see whether the same written using the loop (or iterate:iter) macros would still be as slow.
(defun nth-fibonacci (n &optional (a 0) (b 1))
(if (= n 0)
a
(nth-fibonacci (- n 1) b (+ a b)))) (defun tail-fib (n &optional (v0 0) (v1 1)
(cond ((zerop n) v0)
(t (tail-fib (1- n) v1 (+ v0 v1)))))https://lisp-lang.org/learn/functions
I know the reason for the time taken is because it’s not tail recursive, and that making it tail recursive would make it almost immediate, but for me that was not the point.
The thing is, I don’t really know how to write performant rust code, and yet my naive implementation in rust works better than the example in learn-lisp.
To me that means performant Lisp is non trivial (meaning you need deep understanding to achieve it). If you show a slow but easier to understand example, it’s because fast examples are way harder to understand.
Assuming your rust version is not tail-recursive, this is not true. Of course a tail-recursive (or iterative for that matter) version would be way faster, but that is independent of the language used[1]. The naive algorithm in both Rust and Lisp should be relatively similar in runtimes, and for me (after I properly enabled optimiations) they were.
Also, tail recursion is a red-herring here for another reason. For both Rust and Lisp, I would use an iterative approach, not a tail-recursive approach, like the example in the Rust num_bigint docs[2].
1: A theoretical language that automatically memoized pure functions would make this false, but that doesn't apply to the current discussion.
2: https://docs.rs/num-bigint/0.4.2/num_bigint/
The equivalent Lisp would be:
CL-USER> (defun fib (n)
(let ((f0 0) (f1 1))
(loop repeat n
do (shiftf f0 f1 (+ f0 f1)))
f0))
FIB
CL-USER> (time (fib 1000))
Evaluation took:
0.000 seconds of real time
0.000171 seconds of total run time (0.000136 user, 0.000035 system)
100.00% CPU
471,103 processor cycles
65,456 bytes consed
43466557686937456435688527675040625802564660517371780402481729089536555417949051890403879840079255169295922593080322634775209689623239873322471161642996440906533187938298969649928516003704476137795166849228875
CL-USER>
For small values the difference is lost in the noise. For larger values (e.g. 1000000) Rust is about 2x faster than SBCL, which is about what I would expect for a test like this.here's the Rust code: fn fib(n: usize) -> u64 { match n { 0 => 1, 1 => 1, _ => fib(n - 1) + fib(n - 2), } }
fn main() {
println!("{}", fib(50));
}
Here's the Lisp code: (defun fib (n)
"Return the nth Fibonacci number."
(if (< n 2)
n
(+ (fib (- n 1))
(fib (- n 2)))))
Again, I'm not trying to benchmark the languages, I'm not interested in this language drag racing competition/flamewar, I don't care about how performant a language is in the end. I care about how fast a language is for unit of time I spent. This metric is obviously only useful to me, because if you know a lot of Lisp but little Rust, your time spent will be very different.I also know the Rust snippet will crash at fib(~100), and that I could write the thing in a loop and get there under 1ms. The same is surely true about Lisp.
Factorial and Fibonacci as a pair are good introductions to the issue of recursion--and lisp had recursion back in the dark ages before other languages did--but they're aren't what lisp is about.
so, were some other newbie to read your comment, it might really put them off learning lisp for entirely the wrong reasons.
cheers :)
It's going to make a lot of memory allocations and make a lot of uninlinable function calls, both of which are places that I would expect rust to have an advantage. That being said, I'd be curious to see your rust version, as just the number of bignum operations done for fib(50) is going to take a long time.
I wasn’t benchmarking CL vs Rust, but my CL vs my Rust. What I was trying to see is, can I do something useful with this? Or do I need to invest a good amount of time (something I don’t really have now) before being productive.
As for the rust version, I did the “trivial” (and terrible) translation. Match on input, recursive call on the _ arm.
Lisp (SBCL):
(defun fib (n)
(case n
(0 0)
(1 1)
(otherwise (+ (fib (- n 1)) (fib (- n 2))))))
(time (fib 40))
Rust: use num_bigint::BigUint;
use num_traits::{Zero, One};
// Calculate large fibonacci numbers.
fn fib(n: usize) -> BigUint {
match n {
0 => Zero::zero(),
1 => One::one(),
_ => fib(n-1) + fib(n-2),
}
}
fn main() {
println!("fib(40) = {}", fib(40));
}I did not mean to say that I believe it inferior or anything, just that I felt writing performant code in Lisp is non trivial to learn, while rust feels pretty natural (to me).
I’ll try again at some point, but I don’t think it’s a wise investment of time at this point, I’d rather be “fluent” in rust first, then maybe try lisp.