Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Timer code does not work!

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Timer code does not work!

    Hi,
    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.
    Part 2: Imagine you need to do something similar (timing of method calls) for many places in an application. What other ideas come to mind, e.

    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,
    Maria.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Timer code does not work!

    Quote Originally Posted by mariapatrawala View Post
    Hi,
    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?
    Because calling a function which increments a single variable takes less time than a millisecond.

Similar Threads

  1. How to Use Timer in Java
    By neo_2010 in forum Java SE API Tutorials
    Replies: 2
    Last Post: August 6th, 2013, 09:49 AM
  2. WHY this code dont work?
    By sibbe in forum Java Theory & Questions
    Replies: 7
    Last Post: December 9th, 2010, 10:47 AM
  3. please tell me why this code does not work
    By amr in forum Java Theory & Questions
    Replies: 9
    Last Post: December 6th, 2010, 06:46 PM
  4. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  5. Timer?
    By TimW in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 27th, 2009, 07:43 AM