Results 1 to 3 of 3

Thread: System Clock

  1. #1
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default System Clock

    I am running 2 sorting algorithms (Merge and Insertion) on a series of integer arrays. My assignment is to run the algorithms and do an analysis on the efficiency of each one. I am supposed to use the system clock to time the algorithms, but for some reason, no measurable time seems to be elapsing, at least from the system clock perspective. I am using

    Java Code:
        public static String now(String dateFormat) {
            Calendar cal = Calendar.getInstance();
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
            return sdf.format(cal.getTime());
    
        }
    so when I need to get the time I am setting my time string(startAlgorithm and stopAlgorithm) to:

    now("H:mm:ss:SSS");

    Problem is They are both coming back with the same time, and I am setting the strings at the beginning and the end of the sort(which involves loops and calls to other methods so I know time is elapsing. I don't think I can get any more precision than SSS (millilseconds), can I?

  2. #2
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    For measuring elapsed time, try System.nanoTime().

    When timing code it's usually also worth running your algorithms multiple times and alternating between them to even out fluctuations due to caching, system performance, garbage collection, etc. It's easy to end up measuring the time it takes to do stuff that's actually quite unrelated to your code.

  3. #3
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

Similar Threads

  1. 24 hour clock to 12 hour clock project.
    By bs3ac in forum New To Java
    Replies: 4
    Last Post: 01-08-2013, 10:10 AM
  2. clock
    By ws6driver in forum New To Java
    Replies: 1
    Last Post: 07-31-2009, 04:15 AM
  3. Desktop Clock
    By olamide in forum Threads and Synchronization
    Replies: 1
    Last Post: 03-19-2009, 05:28 PM
  4. Need help with Java clock
    By adz666 in forum New To Java
    Replies: 1
    Last Post: 10-15-2008, 02:18 AM
  5. Help please in digital clock
    By jaidod in forum Java Applets
    Replies: 1
    Last Post: 04-17-2008, 04: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
  •