Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
schwarmco807yyou can save some calculation (if that helps) like this:
if (x > 0) {
x -= friction;
if (x < 0) {
x = 0.0f;
}
} -
ChappIO46937ytoZero (x, friction) {
return (abs (x) - friction) * sign (x)
}
If you don't have a sign then:
sign (x) {
return x == 0 ? 0 : x / abs (x)
} -
Based on some of the optimizations presented, all of which are just trying to make the code shorter, aren't actually helping with this code's performance.
As @ianertson said, this is a calculation for a 3D application which is performed every frame, i.e. executed a crap tonne of times per second!
I wrote some of the solutions proposed and performance tested each solution. As I suspected, by introducing an abs(...) function into the mix does "shorten" the code, but at the same time really kills the performance when compared to the original piece of code.
So, sometimes the best optimization is to just leave it be! 😎 -
@schwarmco
Yours is missing the positive case. I didn't write full on unit tests for this exercise, but even though yours looks shorter, it doesn't seem complete.
Improve it!
undefined