-
Notifications
You must be signed in to change notification settings - Fork 92
Speedup C# version by using double type for rounds variable #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
WalkthroughThe rounds variable in Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (2)
src/leibniz.cs (2)
1-1: Consider keepingroundsas an integer iteration count
roundsis conceptually “number of iterations”, so usingdoublehere is non‑idiomatic and also permits non‑integral values fromrounds.txt. Given it’s only used as a loop bound, anintkeeps intent clearer and avoids surprising inputs.If your benchmarks still show a win with
double, it’d be good to document that in a comment; otherwise I’d keep this as anint:-var rounds = double.Parse(File.ReadAllText("rounds.txt")); +var rounds = int.Parse(File.ReadAllText("rounds.txt"));As per coding guidelines, aim for idiomatic code in
src/leibniz.*.
6-9: Avoid mixingintloop index with adoubleupper boundWith
var i = 2;inferred asintandroundsasdouble, the conditioni < rounds + 2forces an implicit numeric conversion on every iteration. That may offset or reverse any performance gain you’re aiming for and hurts readability a bit.Two cleaner options:
- Keep everything integral (most idiomatic for counts):
-var rounds = double.Parse(File.ReadAllText("rounds.txt")); +var rounds = int.Parse(File.ReadAllText("rounds.txt")); -for (var i = 2; i < rounds + 2; i++) { +for (var i = 2; i < rounds + 2; i++) { x = -x; pi += x / (2 * i - 1); }
- Or, if you really want a
double-based loop, make the indexdoubletoo:var rounds = double.Parse(File.ReadAllText("rounds.txt")); for (double i = 2.0; i < rounds + 2.0; i += 1.0) { x = -x; pi += x / (2.0 * i - 1.0); }I’d benchmark both variants; integer bounds are usually friendlier to the JIT for tight loops.
As per coding guidelines, favor clear, idiomatic constructs in
src/leibniz.*.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
src/leibniz.cs(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/leibniz.*
📄 CodeRabbit inference engine (CLAUDE.md)
src/leibniz.*: Create source file following the naming conventionsrc/leibniz.<ext>when adding a new language implementation
Implementations must be single-threaded with no concurrency, parallelism, multi-threading, async, or parallel processing
All implementations must use the Leibniz formula for calculating π
Use idiomatic code with standard language features; compiler optimization flags are allowed, and auto-vectorization hints like@simdin Julia are acceptable
Files:
src/leibniz.cs
⚙️ CodeRabbit configuration file
src/leibniz.*: This is a benchmark implementation using the Leibniz formula to calculate π.
Review for:
- Single-threaded execution only (no concurrency/parallelism)
- Correct Leibniz formula implementation
- Potential performance optimizations that don't involve parallelism
- SIMD is allowed but should be in separate files (e.g., leibniz_simd.*)
Files:
src/leibniz.cs
🧠 Learnings (2)
📓 Common learnings
Learnt from: CR
Repo: niklas-heer/speed-comparison PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-14T06:40:02.627Z
Learning: Applies to src/leibniz.* : Use idiomatic code with standard language features; compiler optimization flags are allowed, and auto-vectorization hints like `simd` in Julia are acceptable
Learnt from: CR
Repo: niklas-heer/speed-comparison PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-14T06:40:02.627Z
Learning: Applies to src/leibniz.{c,cpp,cc,cxx,h,hpp} : In C/C++ implementations, declare variables inside loops to enable auto-vectorization
Learnt from: CR
Repo: niklas-heer/speed-comparison PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-14T06:40:02.627Z
Learning: Applies to src/leibniz.* : All implementations must use the Leibniz formula for calculating π
📚 Learning: 2025-12-14T06:40:02.627Z
Learnt from: CR
Repo: niklas-heer/speed-comparison PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-12-14T06:40:02.627Z
Learning: Applies to src/leibniz.* : Use idiomatic code with standard language features; compiler optimization flags are allowed, and auto-vectorization hints like `simd` in Julia are acceptable
Applied to files:
src/leibniz.cs
🧬 Code graph analysis (1)
src/leibniz.cs (1)
src/leibniz.js (1)
rounds(4-4)
|
FYI this tanks performance on ARM64. It's worth a try but I think having the submissions optimize for the runner hardware is flawed. I guess other languages may/will do it also, but it's unfortunate since it makes results less interesting because adding very specific changes may confuse casual readers which in practice won't be able to apply the same insight. |
This implementation was consistently faster in my testing.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.