Help / Release / J 6.02 / Integer Division and Remainder Improved

From J Wiki
Jump to navigation Jump to search


>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Release



Integer Division and Remainder Improved

initial writing: 2006-10-26
last updated: 2007-10-23


The following dyads have been made faster and/or leaner on (machine word) integer arguments, with the largest improvement occurring when the divisor is a positive power of 2 and the dividend is non-negative.

x <.@% d     integer quotient (also x >.@% d and @: instead of @)
d|x  integer remainder
(0,d)#:x  integer quotient and remainder

The following benchmarks demonstrate the improvement.

ts=: 6!:2 , 7!:2@]
x=: 1e6 ?@$ 2e5 
ts 'Expression'
Expression    J 6.02    J 6.01    Ratio
x <.@% 256  0.00557535 4.19507e6 0.15662585 2.09723e7 28.09 5.00
x <.@%  17 0.02155449 4.19507e6 0.18823105 2.09723e7  8.73 5.00
  2 | x  0.00322822 1.04915e6 0.02589772 4.19488e6  8.02 4.00
256 | x  0.00610019 4.19494e6 0.03139283 4.19494e6  5.15 1.00
 17 | x  0.02469388 4.19494e6 0.02866373 4.19494e6  1.16 1.00
0 256 #: x  0.00956875 8.38925e6 0.06542851 8.38925e6  6.84 1.00
0  17 #: x  0.04244978 8.38925e6 0.06992786 8.38925e6  1.65 1.00



>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Release