Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6715823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:39:38+00:00 2026-05-26T08:39:38+00:00

I wrote a simple Stopwatch class as follows. public class StopWatch { private long

  • 0

I wrote a simple Stopwatch class as follows.

public class StopWatch 
{
  private long startTime;
  private long stopTime;
  private boolean isRunning;


  public void Start()
 {
this.startTime=System.currentTimeMillis();
this.isRunning=true;

 }

 public void Stop()
 {
this.stopTime=System.currentTimeMillis();
this.isRunning=false;

 }


public long timeElapsed()
{
if(isRunning)
{
    return ((System.currentTimeMillis()-startTime));

}
else
{
    return ((stopTime-startTime));
}



 }
 public static void main(String[] args) 
 {
StopWatch s=new StopWatch();
try
{
s.Start();
int sum=0;
for( int i=0;i<=10;i++)
{

sum=sum+i;  

}
s.Stop();
long timetaken=s.timeElapsed();
System.out.println(timetaken);
}
catch(Exception e)
{
    System.out.println(e.getMessage());
}
 }  




  }

If I add +100L to stop time then I get answer as 100. For some reason both starttime and stoptime have the same value and the subtraction is 0. Not sure why that is happening.

I had also put 100000 as the loop counter and even that had given me 0. While copy pasting code the value was 10 in for loop.

With nanoseconds it’s working fine, I was trying to check for performance issues that come with autoboxing in Java.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:39:38+00:00Added an answer on May 26, 2026 at 8:39 am
    for( int i=0;i<=10;i++) {  sum=sum+i;    } 
    

    The execution time of the above code is less than a millisecond. Try with System.nanoTime()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

so i wrote a simple timer class public class ConsoleTimer : IDisposable { private
I wrote a simple webpage as follows: <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Strict//EN
I wrote simple GUI page: public class Test01 { RootPanel r = RootPanel.get(); public
I wrote a simple implementation class: public interface MyRemote extends Remote { public String
I have a very simple StopWatch application in Silverlight. I have the following private
Im new on Dxpreince 10.(and don't think im expert on this) I wrote simple
I wrote this simple test code, by adapting a piece from a book, to
I wrote a simple program that using Class::ArrayObjects but It did not work as
I wrote simple class in JS witch works, but i had problem when i
is possible this wrote simple, code is here: foreach (var friend in friends) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.