This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Performance and the fly likes efficient remainder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Performance
Bookmark "efficient remainder" Watch "efficient remainder" New topic
Author

efficient remainder

manolis tsamis
Greenhorn

Joined: May 12, 2013
Posts: 3
Hallo! Is there any way to find fast if a division has remainder or not. I don't want to know the remainder, just if there is. Thanks in advance.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

manolis tsamis wrote:Hallo! Is there any way to find fast if a division has remainder or not. I don't want to know the remainder, just if there is. Thanks in advance.


How about the modulus operator?

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
manolis tsamis
Greenhorn

Joined: May 12, 2013
Posts: 3
I am currently using that but because it's repeated million times modulus is slowing it down... I was searching for another thing to do
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Checking the java virtual machine book we see that there are opcodes irem and lrem to get the remainder for int and long division respectively.

I don't think you are going to get faster than a single opcode.

Bill
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18538
    
  40

William Brogden wrote:Checking the java virtual machine book we see that there are opcodes irem and lrem to get the remainder for int and long division respectively.

I don't think you are going to get faster than a single opcode.


... unless the OP can figure out a new algorithm that doesn't need to do the operator millions of times.

Henry
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

manolis tsamis wrote:I am currently using that but because it's repeated million times modulus is slowing it down... I was searching for another thing to do

Are you absolutely sure that it's the modulus that's slowing you down? Most attempts at efficiency end up focusing on the wrong thing, so you'd better be able to prove that its the '%' that's causing all your problems before you start looking for a "better %".

That said, there are faster ways of doing it, the most basic being if d (your divisor) is a power of two:
x % d == x & (d-1)

but you'd better be awfully sure it's going to help before you start littering your program with arcane crap like that.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: efficient remainder
 
Similar Threads
gridbaglayout
Calculator %
Object expected error on IE
GridBagLayout problems
Tutorials on Listeners