Is there a way of timing how long it takes between a piece of code executing to when it finishes and then displaying this time? I would like to display a clock which begins timing when a program starts running and stops when it finishes.


Recommended Answers

All 8 Replies

just add a timer at the start of the program execution .... there are two types of timers .... one for gui and one is general
Timer t = new Timer();

Timer t = new Timer(ActionListener, delay);

Read more about the timers in the java API.

Thanx a lot for ur suggestions, been a great help! :D

An alternative solution, I've used to as a cheap profiling trick in big programs:

long startTime = System.currentTimeMillis(); // time at start in millisecs


long stopTime = System.currentTimeMillis(); // time at end

// time elapsed is stopTime-startTime

See this link for details.

it display in this format
How to display in time format?

What you see is in milliseconds. When you get the difference, multiply by 1000 and you will have how many seconds have passed

I mean, I want to display it in hh:mm:ss
Can anyone suggest?

You have a value in milliseconds. You can either use SimpleDateFormat, or do it yourself using the following rules:
1 sec = 1000 millisec
1 min = 60 sec
1 hour = 60 min

And sorry about my last post, I made a very big mistake: You must divide by 1000 to get the seconds

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.