The Artima Developer Community
Sponsored Link

Java Answers Forum
using timers

4 replies on 1 page. Most recent reply: Apr 11, 2003 7:19 PM by jake

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

using timers Posted: Apr 11, 2003 3:32 PM
Reply to this message Reply
Advertisement
Ok so I have implemented two methods that do two different types of searches. One binarySearch() and the other is sequentialSearch(), both are going to be searching the same array for a random number. I need to time both of them to see which one is faster. The teacher recomends System.currentTimeMillis() but I don't know how to use that, much less use a timer. I know they are simple can someone just give me a short crash course in timers.


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using timers Posted: Apr 11, 2003 5:12 PM
Reply to this message Reply
The nomenclature is a bit confusing; you wouldn't the kind of timer that generates timer events for this sort of thing, instead you use System.currentTimeMillis(). Something like this:

   /**
    * Returns the elapsed time for this thing to run.
    */
   public static int timeThis( Runnable runner )
   {
      long start = System.currentTimeMillis();
      runner.run();
      return System.currentTimeMillis() - start;
   }

Now you just pass a Runnable object to this method, it will run it and return the time it took to run in milliseconds.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using timers Posted: Apr 11, 2003 5:13 PM
Reply to this message Reply
Oops, I didn't compile this, I just typed it in, but I noticed right after the post, that the method should return long, not int, of course.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: using timers Posted: Apr 11, 2003 6:06 PM
Reply to this message Reply
Here's an example:

import java.util.*;
 
public class TimeTest implements Runnable
{
   private static Random random = new Random();
 
   private List list;
   private final int itemCount;
   
   public TimeTest( List list, int itemCount )
   {
      this.list = list;   // Should also assert(list != null);
      this.itemCount = itemCount;
   }
 
   public void run()
   {
      for( int i = 0; i < itemCount; i++ )
         list.add( new Integer( random.nextInt() ) );
 
      // Fool with the list:
      for( int i = 0; i < itemCount; i++ )
      {
         // Randomly choose to remove from the end or begining:
         int where = random.nextInt(2) > 0 ? 0 : list.size()-1;
 
         // Also randomly choose whether to add or remove an item:
         if( random.nextInt(2) > 0 )
            list.add(where, new Integer(where));
         else
            list.remove(where);
      }
   }
 
   public static long timeThis( Runnable runner )
   {
      long start = System.currentTimeMillis();
      runner.run();
      return System.currentTimeMillis() - start;
   }
 
   public static void main( String [] args )
   {
      int itemCount = 50000;
      for( int i = 0; i < 10; i++ )
      {
         long t = timeThis( new TimeTest( new ArrayList(), itemCount ) );
         System.out.print( "Test " + (i+1) +": ArrayList took " + t + " milliseconds, " );
   
         t = timeThis( new TimeTest( new LinkedList(), itemCount ) );
         System.out.println( "LinkedList took " + t + " milliseconds." );
      }
   }   
}

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: using timers Posted: Apr 11, 2003 7:19 PM
Reply to this message Reply
thanks, thats perfect!

Flat View: This topic has 4 replies on 1 page
Topic: java internetworking Previous Topic   Next Topic Topic: help with  simple method code.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use