Results 1 to 6 of 6
  1. #1
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default Question about System.nanoTime()?

    Hi everyone,

    I'm working a problem that I need to calculate runtime complexity of a particular function. However, I have some troubles understand how Java's API "nanoTime()" works. When I tried this example, each time I run it, I got different result. I think it should be the same every time, but it was not. Can anyone help me explain how this function works? and how to calculate runtime of a particular function?

    Thanks,

    Java Code:
    public static void main( String[] args )
    	{
    		
    		long beg = System.nanoTime();
    		for( int i = 0; i < 10000; ++i )
    		{
    			if( i == 1 )
    				System.out.print( "\n" );
    		}
    		long end = System.nanoTime();
    		
    		System.out.println( end - beg );
    	}

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Why should it be the same every time?

    It's not going to be. Runtimes for a program will vary depending on the load on the rest of the machine at the time this program runs. And, even when the load is the same, on most machines it will still vary, as many of the programs that generally run in the background will sit idel most of the time, then activate and do something, and then sit idle again, and the timing of that can, and will, cause the runtime for your program to vary.

  3. #3
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi masikade,

    Thanks for your quick response, so what can I do to get rid of those dependent factors since I need to perform the time complexity ( O(n), O(logN) ) of several data structures. What is a reasonable approach that I should do in this case?

    Thanks,

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Run them about 1500 times and take the average.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Even doing that will result in misinterpreted data.
    Java Code:
    23.71333
    4.57842
    3.17717
    3.11045
    3.54674
    Java Code:
    21.45492
    4.30125
    60.75131
    2.3662
    3.09505
    (I had a third test but its data got deleted. It was something like 95.5, 3.2, 1.67, 1.65, 1.65.)

    Those sets of data are generated by this:
    Java Code:
    		for (int j = 0; j < 5; j++) {
    			long beg = System.nanoTime();
    			for( int i = 0; i < 100000; ++i )
    			{
    				if( i == 1 )
    					System.out.print( "\n" );
    			}
    			long end = System.nanoTime();
    
    			System.out.println( (double)(end - beg) / 100000.0 );
    		}
    Even averaging the times over 100,000 iterations does not provide an accurate performance check on the data.

    You will be best to run it in an isolated environment--no background programs, using javac rather than any IDE you may be using. And make sure you run the test multiple times for each set of iterations (as you can see, mine is 5 sets of 100,000). That way you get one set of data when the program starts, then once it's cooling off, and so on.

  6. #6
    chan_nguyen is offline Member
    Join Date
    Sep 2010
    Posts
    13
    Rep Power
    0

    Default

    Hi there,
    Thanks a lot for you guys' helps. I think I understand my problem; I can't use these methods to compare run time of a particular operation of a data structrue ( e.g TreeMap or HashMap ). Because time complexity like big O is about the number of times comparison not about running time. I can measure the time of TreeMap.put() operation but this won't give me any further details related to its average time complexity O(logN). I think I should talk to my professor about this assignment cause we can't perform an close approximation in this case because of many dependent factors.

    Thanks,

Similar Threads

  1. System.nanoTime();
    By rootpi in forum New To Java
    Replies: 6
    Last Post: 01-28-2011, 04:38 AM
  2. System ClassPath and compiler question
    By Moncleared in forum New To Java
    Replies: 1
    Last Post: 02-23-2010, 03:45 AM
  3. Replies: 3
    Last Post: 02-07-2010, 09:34 PM
  4. Question about System.in/out/err
    By suvojit168 in forum New To Java
    Replies: 3
    Last Post: 07-26-2009, 04:01 AM
  5. problem with System.nanoTime()
    By Bryan500 in forum New To Java
    Replies: 5
    Last Post: 11-01-2008, 04:39 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
  •