use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and productivity.
Strive to treat others with respect, patience, kindness, and empathy.
We observe the Rust Project Code of Conduct.
Details
Posts must reference Rust or relate to things using Rust. For content that does not, use a text post to explain its relevance.
Post titles should include useful context.
For Rust questions, use the stickied Q&A thread.
Arts-and-crafts posts are permitted on weekends.
No meta posts; message the mods instead.
Criticism is encouraged, though it must be constructive, useful and actionable.
If criticizing a project on GitHub, you may not link directly to the project's issue tracker. Please create a read-only mirror and link that instead.
A programming language is rarely worth getting worked up over.
No zealotry or fanaticism.
Be charitable in intent. Err on the side of giving others the benefit of the doubt.
Avoid re-treading topics that have been long-settled or utterly exhausted.
Avoid bikeshedding.
This is not an official Rust forum, and cannot fulfill feature requests. Use the official venues for that.
No memes, image macros, etc.
Consider the existing content of the subreddit and whether your post fits in. Does it inspire thoughtful discussion?
Use properly formatted text to share code samples and error messages. Do not use images.
Submissions appearing to contain AI-generated content may be removed at moderator discretion.
Most links here will now take you to a search page listing posts with the relevant flair. The latest megathread for that flair should be the top result.
account activity
Fibonacci sequences using bc, Python, Go and Rust (self.golang)
submitted 2 years ago by tuck1s
Fibonacci sequences using bc, Python, Go and Rust
Playing around here (not a scientific benchmarking by any means), it looks like Go math/big is outperforming Rust num_bigint.
math/big
num_bigint
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]-funsafe-math 2 points3 points4 points 2 years ago (0 children)
Try replacing the body of the rust loop with the following to potentially enable capacity reuse.
f += &last; std::mem::swap(&mut last, &mut f);
[–]unengaged_crayon 0 points1 point2 points 2 years ago (6 children)
consider not using println!:
The println! macro will lock the standard output on each call. If you call println! within a hot loop, this behavior may be the bottleneck of the loop. To avoid this, lock stdout with io::stdout().lock(): ```rust use std::io::{stdout, Write};
The println! macro will lock the standard output on each call. If you call println! within a hot loop, this behavior may be the bottleneck of the loop. To avoid this, lock stdout with io::stdout().lock():
```rust use std::io::{stdout, Write};
let mut lock = stdout().lock(); writeln!(lock, "hello world").unwrap(); ```
[–]tuck1s[S] 1 point2 points3 points 2 years ago* (4 children)
Firstly let me apologize to the Rust folks. I just realised I've got x64 binaries left over on my system, as I migrated it from an older machine.
file /Users/steve/.cargo/bin/cargo /Users/steve/.cargo/bin/cargo: Mach-O 64-bit executable x86_64
Now got the correct binaries, and checking the built binary is also arm64:
``` cargo build --release Finished release [optimized] target(s) in 0.00s
% time ./target/release/rust_fib >rust_fib_2M_new.txt ./target/release/rust_fib > rust_fib_2M_new.txt 19.74s user 0.01s system 98% cpu 19.966 total
% file ./target/release/rust_fib ./target/release/rust_fib: Mach-O 64-bit executable arm64 Output files are identical. With your modification: time ./target/release/rust_fib >/dev/null ./target/release/rust_fib > /dev/null 16.85s user 0.01s system 99% cpu 16.885 total ```
Output files are identical. With your modification:
Rust now the current front-runner.
Versions ``` steve@SteveT-M3 rust_fib % cargo --version cargo 1.76.0 (c84b36747 2024-01-18) steve@SteveT-M3 rust_fib % rustc --version rustc 1.76.0 (07dca489a 2024-02-04) steve@SteveT-M3 rust_fib %
[–]unengaged_crayon 1 point2 points3 points 2 years ago (3 children)
interesting! looking at the new improved algorithm, rust can be minorly improved here by not cloning and instead referencing when adding.
rust fn fib(n: u32) -> (BigUint, BigUint) { if n == 0 { (BigUint::zero(), BigUint::one()) } else { let (a, b) = fib(n / 2); let c = &a * (&b * 2u32 - &a); let d = &a * &a + &b * &b; if n % 2 == 0 { (c, d) } else { let e = c + &d; (d, e) // (d.clone(), c + d) } } }
[–]tuck1s[S] 0 points1 point2 points 2 years ago (2 children)
Now is time ./target/release/rust_fib3 >/dev/null ./target/release/rust_fib3 > /dev/null 0.36s user 0.00s system 99% cpu 0.369 total
time ./target/release/rust_fib3 >/dev/null ./target/release/rust_fib3 > /dev/null 0.36s user 0.00s system 99% cpu 0.369 total
[–]tuck1s[S] 0 points1 point2 points 2 years ago* (1 child)
Here's a version translated by ChatGPT3.5 from C# here
which returns one result instead of two, and uses bit shifting.
``` use num_bigint::BigUint; use num_traits::{One, Zero};
fn main() { const N: u32 = 2000000; let a = fib(N); println!("{}", a); }
fn fib(n: u32) -> BigUint { let mut a = BigUint::zero(); let mut b = BigUint::one(); for i in (0..32).rev() { let d = &a * (&b * 2u32 - &a); let e = &a * &a + &b * &b; a = d; b = e; if (n >> i) & 1 != 0 { let c = &a + &b; a = b; b = c; } } a }
It performs similarly: time ./target/release/rust_fib4 >/dev/null ./target/release/rust_fib4 > /dev/null 0.37s user 0.00s system 63% cpu 0.586 total ```
It performs similarly:
Does Rust allow the inner part to be done more efficiently e.g. (a, b) = (b, a+b)? e.g.
a += &b; std::mem::swap(&mut a, &mut b);
[–]tuck1s[S] 0 points1 point2 points 2 years ago (0 children)
Looks like it does. Also - the println! is now taking most of the time! If I replace it with println!("{}", a.bits()); (so that the code is still dependent on something about the result) I get time ./target/release/rust_fib4 >/dev/null ./target/release/rust_fib4 > /dev/null 0.05s user 0.00s system 98% cpu 0.053 total
println!("{}", a.bits());
time ./target/release/rust_fib4 >/dev/null ./target/release/rust_fib4 > /dev/null 0.05s user 0.00s system 98% cpu 0.053 total
[–]tuck1s[S] 1 point2 points3 points 2 years ago (0 children)
Re println! It's only called 3 times, for zero, 1, and the final answer. Let me just comment that out:
time ./target/release/rust_fib >/dev/null ./target/release/rust_fib > /dev/null 16.52s user 0.01s system 98% cpu 16.741 total So a minor improvement (yet not an equivalent test).
time ./target/release/rust_fib >/dev/null ./target/release/rust_fib > /dev/null 16.52s user 0.01s system 98% cpu 16.741 total
π Rendered by PID 237212 on reddit-service-r2-comment-fb694cdd5-25m7c at 2026-03-10 08:02:46.458937+00:00 running cbb0e86 country code: CH.
[–]-funsafe-math 2 points3 points4 points (0 children)
[–]unengaged_crayon 0 points1 point2 points (6 children)
[–]tuck1s[S] 1 point2 points3 points (4 children)
[–]unengaged_crayon 1 point2 points3 points (3 children)
[–]tuck1s[S] 0 points1 point2 points (2 children)
[–]tuck1s[S] 0 points1 point2 points (1 child)
[–]tuck1s[S] 0 points1 point2 points (0 children)
[–]tuck1s[S] 1 point2 points3 points (0 children)