Skip to content

Let bindings result in different memory and cpu usage. Why? #541

Closed Answered by fallen-icarus
fallen-icarus asked this question in Q&A
Discussion options

You must be logged in to vote

I think I figured it out. Since add_ratios2 called x.denominator and y.denominator twice, I did another test with this version:

pub fn add_ratios3(x: Rational, y: Rational) -> Rational {
  let y_den =
    y.denominator
  let x_den =
    x.denominator
  let new_num =
    x.numerator * y_den + y.numerator * x_den
  let new_den =
    x_den * y_den
  let gcd =
    euclid(new_num, new_den)

  Rational { numerator: new_num / gcd, denominator: new_den / gcd }
}

In this version, I stored the result of x.denominator and y.denominator in a variable and then used that variable twice instead of calling those functions twice. This version resulted in similar performance to the first version with all …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@rvcas
Comment options

rvcas May 25, 2023
Collaborator

Answer selected by fallen-icarus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants