int - How do you implement the remainder method % for integers in java? How fast is it? -
naive implementation: if want find p % q, subtract q p until number < q. takes p/q subtractions, , p/q comparisons.
how java this, , how fast it?
when java compiler sees % operator, generates 1 of typed rem bytecode instructions, e.g. irem, lrem, frem, drem, etc. actual instruction depends on type. 2 ints instruction irem.
these instructions interpreted jvm, producing cpu actions obtaining remainder.
most cpus these days (at least, ones capable of running java) have built-in instructions take divisor , dividend, , produce quotient , remainder pair. why % operator fast division operator /.
see this q&a information on how instruction may implemented in cpu.
Comments
Post a Comment