2

The leetcode problem Sum of Two Integers reads

Given two integers a and b, return the sum of the two integers without using the operators + and -.

However, I did use the + operator and my solution was just a + b.

My submission was slower than 10% of submissions and used more memory than 21% of other submissions - is this just other people doing the same thing and getting more lucky on the benchmarks or am I missing something?

Thanks

nectarine
  • 65
  • 4
  • What are the types? If they're `i32` or `i64` then you can't beat `+`. If they're some bignum data structure, then `+` is creating a (potentially significant) temporary value, and you may be able to outperform it by mutating `a` in place, i.e. `a += b`. – Silvio Mayolo Feb 24 '23 at 15:15
  • Both i32, interestingly if you do `a += b; a` the results are better in terms of runtime but worse in memory usage beats 100% in runtime speed, beats only 58.6 in memory usage, strange – nectarine Feb 24 '23 at 15:22
  • This question should be on https://codegolf.stackexchange.com/ and not here, but the "close" option doesn't come up with that as a suggested option. Either way, ask there please. – Kevin Anderson Feb 24 '23 at 15:32
  • my question got locked on codegolf.stackexchange.com and I was told to post on here 0_0 – nectarine Feb 24 '23 at 16:44
  • The other solutions may be using [faster IO routines](https://stackoverflow.com/questions/56449027/fastest-idiomatic-i-o-routine-in-rust-for-programming-contests) to read the integers from stdin (or the input file) and to write the answer to stdout (or the output file). – EvilTak Feb 24 '23 at 17:16

0 Answers0