Results 1 to 3 of 3
  1. #1
    tabchas's Avatar
    tabchas is offline Member
    Join Date
    Mar 2011
    Location
    Austin
    Posts
    60
    Rep Power
    0

    Default Best way to Time Execution

    Hey Guys,

    I am currently going through an Algorithms and Data Structures Book and have written code for a simple Bubble Sort and Selection Sort. However, I want to see the difference in speeds between the two. Currently, they are both in separate methods and I want to test the speed of the methods itself before I call it till after it returns. What is the best way to do this? I was looking online but realized that using a System call to currentTimeMillis() returned a value of 0 for both. What is the most accurate way to measure execution time? I want to be able to time my code from now on so I need the best way and the most versatile so it works on any occasion.

    Thanks so much!
    Tabish Chasmawala

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,519
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by tabchas View Post
    I am currently going through an Algorithms and Data Structures Book and have written code for a simple Bubble Sort and Selection Sort. However, I want to see the difference in speeds between the two. Currently, they are both in separate methods and I want to test the speed of the methods itself before I call it till after it returns. What is the best way to do this? I was looking online but realized that using a System call to currentTimeMillis() returned a value of 0 for both. What is the most accurate way to measure execution time? I want to be able to time my code from now on so I need the best way and the most versatile so it works on any occasion.
    I copied this from the API docs for the System.nanoTime() method:

    Quote Originally Posted by API
    long startTime = System.nanoTime();
    // ... the code being measured ...
    long estimatedTime = System.nanoTime() - startTime;
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    You could also have it perform the operation multiple times and then average it out.

Similar Threads

  1. Replies: 10
    Last Post: 11-29-2010, 02:02 PM
  2. threading execution time question
    By centenial in forum Threads and Synchronization
    Replies: 4
    Last Post: 09-09-2010, 05:49 AM
  3. Need help with Process execution time display
    By murali18 in forum New To Java
    Replies: 5
    Last Post: 03-18-2010, 11:19 PM
  4. Replies: 25
    Last Post: 06-28-2008, 05:08 PM
  5. Method execution time
    By javaplus in forum Advanced Java
    Replies: 3
    Last Post: 11-26-2007, 09:51 AM

Posting Permissions

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