Results 1 to 11 of 11
  1. #1
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default 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,

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    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");
    ???

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    431
    Rep Power
    6

    Default

    Better use System.currentTimeMillis() in your program.
    Mak
    (Living @ Virtual World)

  4. #4
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    @eRaaaa
    Thanks, it did work.

  5. #5
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    @makpandian
    Yet the time diff is 0.

  6. #6
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    431
    Rep Power
    6

    Default

    that does mean that your exe time is less than milli seconds
    Mak
    (Living @ Virtual World)

  7. #7
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    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?

  8. #8
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    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;
    }

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,307
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mariapatrawala View Post
    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;
    }
    Improved in what respect? For one thing: your method doesn't work for values of y < 0 but that's easy to fix.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    Join Date
    Dec 2010
    Posts
    6
    Rep Power
    0

    Default

    Yes and it also won't work for doubles n longs

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,307
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by mariapatrawala View Post
    Yes and it also won't work for doubles n longs
    Of course not because that method takes Integers as parameter types; why not just ints? (primitive); it saves a bunch of (un)boxing operations.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Will this code work
    By rajat16 in forum XML
    Replies: 3
    Last Post: 09-24-2010, 11:52 AM
  2. Why this code don't work?
    By artemff in forum CLDC and MIDP
    Replies: 6
    Last Post: 04-16-2010, 02:57 AM
  3. How does the '*' work in the code?
    By DrMath in forum New To Java
    Replies: 3
    Last Post: 10-28-2009, 09:26 AM
  4. Quit Button, Load new Frame, Timer Code
    By IHateNetbeans in forum New To Java
    Replies: 1
    Last Post: 03-18-2009, 03:58 PM
  5. Replies: 6
    Last Post: 02-06-2009, 08:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •