my dog learned polymorphism*
The moose likes Beginning Java and the fly likes java Modulus 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 » Beginning Java
Bookmark "java Modulus" Watch "java Modulus" New topic
Author

java Modulus

satyam pat
Greenhorn

Joined: Apr 16, 2011
Posts: 14
hey all,

I want to print some message after messageCount(which is incrementing) crosses frequency value say 10;
following works


Can this be done in more effective way ?
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Hi Satyam, not sure if the functionality will suit your needs but there is a way to slightly optimize it.
What is happening with your code is that while loop is getting executed 105 times and for each iteration the "if" condition is being evaluated. Unless you have logic to execute in between the frequency, the following code will reduce the iterations. The difference may be more prominent for large iterations.



Hope this answers your question
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

I doubt it. What Satyam probably omitted was some code that is executed for each iteration. A nested loop may help though:
You'll now get a line printed for 10, 20, 30, ..., 90, 100 and finally 105.
Don't forget to add that j < runs or you will have 5 iterations too many. I've turned the 105 into a final int to make sure that i and j have the same upper bound.


Satyam, I honestly would keep your current code with one modification: it should be messageCount % frequency == 0, or you will get lines printed at 11, 21, etc and not at 10, 20, etc. If I do something like this my guard is similar:
After all, messageCount % frequency is 0 if messageCount is 0 (a case where you don't want to have a line printed) or a non-0 multitude of frequency (a case where you do want to have a line printed).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Satyam, if you are indeed executing some logic for each iteration too , he is a much simpler and cleaner way

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
As Rob has pointed out, you will print something out for 0 messages. Maybe you should start from 1

for (int i = 1; i <= 105; i++) . . .
Pranav Raulkar
Ranch Hand

Joined: Apr 20, 2011
Posts: 73

Campbell Ritchie wrote:As Rob has pointed out, you will print something out for 0 messages. Maybe you should start from 1

for (int i = 1; i <= 105; i++) . . .


correct
satyam pat
Greenhorn

Joined: Apr 16, 2011
Posts: 14
Thanks for all replies.

May be I was not very clear in my question.

while loop and increment part was just simulation for the problem. In the real time message count will be read from database.

so the actual logic was
if (messageCount >= frequency && (messageCount % frequency == 1))
System.out.println("sent message for " + messageCount);

I was looking for way to avoid checking (messageCount >= frequency) every time.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11161
    
  16

Is there some reason you want to take it out? It seems like it is necessary to have it (so you don't print out data the first time around).

Why do you want to get rid of it? What harm is it doing?


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38033
    
  22
. . . and remember a > test is very fast. You can use > rather than >=, which will look neater, but will probably not affect the performance. The > is faster than the % operator.
satyam pat
Greenhorn

Joined: Apr 16, 2011
Posts: 14
my need/requirement changed to send message on first occurrence and then after subsequent frequencies.

which made is really simple to check
if (messageCount % frequency == 1)

btw thanks for the info regarding performance of '>' and '>='
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java Modulus
 
Similar Threads
compareTo method
Java mail attachmetnt include txt extensions
Array
socket performance
rmi: Connection reset