Results 1 to 11 of 11
Thread: Timer java code does not work
- 12-10-2010, 12:04 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
Timer java code does not work
public interface IncDec
{
void increment();
void decrement();
}
public class MyIncDec implements IncDec
{
private int x;
public MyIncDec(int x) {
this.x = x;
}
public void increment() {
this.x++;
}
public void decrement() {
this.x--;
}
}
Part 1: Implement a class which can be used to measure how long each invocation of the increment and decrement method takes (in
milliseconds) and prints out this information. The class should fit in more or less transparently with existing clients of the IncDec interface.
My Solution:
public class Test{
public static void main(String[] args){
MyIncDec mid = new MyIncDec(4);
long l1;
long l2;
l1 = Calendar.getInstance().getTimeInMillis();
mid.increment();
l2 = Calendar.getInstance().getTimeInMillis();
System.out.println(l2-l1);
}
}
My Solution gives me the time diff as 0. When I display the long values before n after calling 'increment', They are the same. Why so?
Thanks,
- 12-10-2010, 12:50 PM #2
Senior Member
- Join Date
- Oct 2010
- Location
- Germany
- Posts
- 787
- Rep Power
- 11
maybe the execution time is < 1 ms ?
Java Code:l1 = System.nanoTime(); mid.increment(); l2 = System.nanoTime(); System.out.println((l2-l1)/(1000*1000d)+" ms");
- 12-10-2010, 01:17 PM #3
Better use System.currentTimeMillis() in your program.
Mak
(Living @ Virtual World)
- 12-11-2010, 09:53 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
@eRaaaa
Thanks, it did work.
- 12-11-2010, 09:55 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
@makpandian
Yet the time diff is 0.
- 12-11-2010, 10:54 AM #6
that does mean that your exe time is less than milli seconds
Mak
(Living @ Virtual World)
- 12-11-2010, 11:17 AM #7
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
Yes....maybe. Suppose if I had to something similar (timing of method calls) for many places in an application. One idea would be to write a separate timer class for all different methods. Does anyone have another optimized idea to get timing more conveniently?
- 12-11-2010, 12:16 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
In what ways can the following code be improved?
public static final Integer multiply(Integer x, Integer y) {
return (y == 0) ? 0 : multiply(x, y - 1) + x;
}
- 12-11-2010, 12:25 PM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
- 12-12-2010, 11:20 AM #10
Member
- Join Date
- Dec 2010
- Posts
- 6
- Rep Power
- 0
Yes and it also won't work for doubles n longs
- 12-12-2010, 11:34 AM #11
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
Similar Threads
-
Will this code work
By rajat16 in forum XMLReplies: 3Last Post: 09-24-2010, 12:52 PM -
Why this code don't work?
By artemff in forum CLDC and MIDPReplies: 6Last Post: 04-16-2010, 03:57 AM -
How does the '*' work in the code?
By DrMath in forum New To JavaReplies: 3Last Post: 10-28-2009, 10:26 AM -
Quit Button, Load new Frame, Timer Code
By IHateNetbeans in forum New To JavaReplies: 1Last Post: 03-18-2009, 04:58 PM -
Running .java-files won't work/compile does!(Code inside)
By wyldstyle in forum New To JavaReplies: 6Last Post: 02-06-2009, 09:05 PM
Bookmarks