groovy-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Winnebeck, Jason" <Jason.Winneb...@windstream.com>
Subject RE: Integer primitive division
Date Fri, 23 Oct 2015 21:24:42 GMT
I can do that, but it does int x = Integer.valueOf(12).intdiv(Integer.valueOf(2)).intValue().
So there is still the overhead of boxing and unboxing, which doesn’t exist for other primitive
operations, and intdiv itself performs some reflection magic as well. What I’m wondering
is if Java is strictly required if performance is a concern with division.

Jason

From: Søren Berg Glasius [mailto:soeren@glasius.dk]
Sent: Friday, October 23, 2015 5:16 PM
To: users@groovy.incubator.apache.org
Subject: Re: Integer primitive division

Hi,

I think you can just do

12.intdiv(2)

it will return a java.lang.Number so you will have to cast it to an java.lang.Integer

Best regards / Med venlig hilsen,
Søren Berg Glasius

Hedevej 1, Gl. Rye, 8680 Ry, Denmark
Mobile: +45 40 44 91 88, Skype: sbglasius
--- Press ESC once to quit - twice to save the changes.

fre. 23. okt. 2015 kl. 22.47 skrev Winnebeck, Jason <Jason.Winnebeck@windstream.com<mailto:Jason.Winnebeck@windstream.com>>:
Is there any way to perform primitive integer division in Groovy compile static mode? If I
do:

(int)(12/2)

Then it coverts 12 and 2 to Integer objects, calls a method that performs BigDecimal division,
then calls “as int” (not just intValue) on the result, which itself does instanceof checks.

I tried intdiv, and it is more efficient, but it still converts to and from Integer objects
so that it can do A.intValue() / B.intValue(), and storing that result in an Integer itself.

I see if I just do 12 + 2, it does a true primitive operation in the bytecode. Is there any
way to perform primitive division in Groovy the same way it is done in Java?

Jason Winnebeck
Software Engineer III Contractor - IT Software Development | Windstream
600 Willowbrook Office Park, Rochester, NY 14450
Jason.Winnebeck@windstream.com<mailto:Jason.Winnebeck@windstream.com> | windstreambusiness.com<http://windstreambusiness.com>
o: 585.794-4585

________________________________
This email message and any attachments are for the sole use of the intended recipient(s).
Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the
intended recipient, please contact the sender by reply email and destroy all copies of the
original message and any attachments.
Mime
View raw message