Skip to content
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

Refactoring and Optimize of multiple copies of same code in DspUtilities.cpp #838

Closed
huberp opened this issue Apr 14, 2019 · 2 comments
Closed

Comments

@huberp
Copy link

huberp commented Apr 14, 2019

Describe the bug
Refering to "DspUtilities.cpp, multiple copies of float rand11 = (((float)rand() / RAND_MAX) * 2.f - 1.f);
a.) Refactor to a function rand11()
b.) Optimize expression to make it less operations
b.1) Create a Constand RAND_MAX_HALF = RAND_MAX / 2
b.2) Make the expression: (((float)rand() / RAND_MAX_HALF) - 1.f);
Take it as a suggestion please: Computer arithmetics has it's subtleties. Not 100% sure whether the "optimized" version as suggested is numerically the same... could be that rounding error might make the range not [ -1, 1 ].

To Reproduce
Stare at the code and do arithmetics ;-)

Expected behavior
Runs with less cpu cycles on every call to any of the funcions in DspUtilities that use the same copy of code.

Screenshots
/

Desktop (please complete the following information):
/

Additional context
/

@baconpaul
Copy link
Collaborator

Thanks. Few questions

  1. Did you focus on this code because of a profile result or just inspection?

  2. I’m surprised the compiler doesn’t do this constant transform since rand max and 2f are constants. Did you peek at the assembly output to check that? If it doesn’t introducing a constexpr halfrandmax could save a mul

  3. If you do make the new function change the new function you propose should be inline and header visible and we would want to check that the ejected code inlines properly on Mac win and lin. Don’t want an extra out of line call!

@baconpaul
Copy link
Collaborator

Oh also if you're interested in performance (which would be great!) #556 is where we've kept a few other optimizations and observations.

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

No branches or pull requests

2 participants