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

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -