Skip to content

CPP: Fibonnaci -> More idiomatic C++ code#367

Merged
PEZ merged 1 commit intobddicken:mainfrom
int2str:main
Jan 19, 2025
Merged

CPP: Fibonnaci -> More idiomatic C++ code#367
PEZ merged 1 commit intobddicken:mainfrom
int2str:main

Conversation

@int2str
Copy link

@int2str int2str commented Jan 18, 2025

Changes from the C-based version:

  • Added command line argument count check
  • C++ does not require "return 0" in the main function
  • Trailing return types
  • Convert argv/argc to std::span to void pointer arithmetic and allow for simpler iteration (if needed)
  • C++23 provides std::print() and std::println() to replace std::cout and friends
  • C++23 ranges and algorithms to clarify intent and to avoid raw for loops. See Sean Parents "No raw for loops" and other sources.

Minor performance tweak:

  • Replaced separate if() checks for n == 0 and n == 1 with single n < 2 check.

Note:
The performance tweak is also applicable to the C version.

Benchmark:
image

@PEZ
Copy link
Collaborator

PEZ commented Jan 18, 2025

Thanks!

Replaced separate if() checks for n == 0 and n == 1 with single n < 2 check.

The C version is considered reference for this. So that means we rather want all contributions to do the two checks. At least until we decide to change the reference. This is not consistently applied so far, but anyway, please keep the two checks for now.

@int2str
Copy link
Author

int2str commented Jan 19, 2025

** DO NOT MERGE **

I've undone the if() checks.
Unfortunately, since C++ is such at the mercy of the compiler optimizing things, there's now a performance regression compared to the previous code and compared to C.
Much as it pains me, I may abandon this pull request if this can't be resolved while creating a more idiomatic C++ solution, instead of merely recompiling C with a C++ compiler...

@int2str
Copy link
Author

int2str commented Jan 19, 2025

Ok, I ran the tests again on my super slow notebook to take some measurement noise out of it. It's very close and certainly in line with the C implementation.

I'll leave it up to you guys whether a few ms regression is worth it to showcase the actual language differences vs. just using the same program with a different compiler. Not sure what's more important here TBH.

Happy to abandon, happy to have it merged now. Your call.

Thx either way.

image

Copy link
Collaborator

@PEZ PEZ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can't have constexpr in there.

@PEZ
Copy link
Collaborator

PEZ commented Jan 19, 2025

Happy to abandon, happy to have it merged now. Your call.

Except for constexpr I'm good with merging all right. You're the CPP expert of us two, and have a bigger say, I would say. 😄

@int2str
Copy link
Author

int2str commented Jan 19, 2025

[[constexpr]] is a suggestion to the compiler, not a rule. It means this function can be invoked in a constexpr context, not that it will.

In this example, since the arguments to the function are a command line argument, it clearly cannot be evaluated at compile time. Further more, the before and after benchmark results show that this has no effect.

Again, just showcasing language features here.
But also happy to remove....

Just let me know.

Changes from the C-based version:
- Added command line argument count check
- C++ does not require "return 0" in the main function
- Trailing return types
- Convert argv/argc to std::span to void pointer arithmetic
  and allow for simpler iteration (if needed)
- C++23 provides std::print() and std::println() to replace
  std::cout and friends
- C++23 ranges and algorithms to clarify intent and to avoid
  raw for loops.
  See Sean Parents "No raw for loops" and other sources.
@int2str
Copy link
Author

int2str commented Jan 19, 2025

Removed constexpr.

@int2str
Copy link
Author

int2str commented Jan 19, 2025

New comparative C/CPP benchmark after removing 'constexpr'

image

@PEZ PEZ merged commit 67aa379 into bddicken:main Jan 19, 2025
1 check passed
@PEZ
Copy link
Collaborator

PEZ commented Jan 19, 2025

Thanks for improving this project, @int2str! 🙏 And thanks for removing constexpr, even though it didn't break any rules after all. It's cleaner this way and we won't have to have that discussion at least. There is no lack of issues to discuss, after all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants