12

Improve it!

Comments
  • 0
    The method is used every frame in a 3D application
  • 3
    Could probably make it shorter, but the logic seems sound! 🤓
  • 1
    Maybe something like this to reduce nesting and logic duplication...
  • 0
    you can save some calculation (if that helps) like this:

    if (x > 0) {
    x -= friction;
    if (x < 0) {
    x = 0.0f;
    }
    }
  • 4
    toZero (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)
    }
  • 0
    @cGF0 don't need the else if you return
  • 4
    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! 😎
  • 0
    @GinjaNinja i had that in mind - have you tested mine?
  • 0
    @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.
Add Comment