Prabhat Yadav
Prabhat Yadav

Reputation: 1331

How to measure the performance of a simple java program using eclipse

I want to determine which program is better and fast in term memory and time used. I faced a problem where I need to declare a variable I have two aproaches

  1. use static variable
  2. use default variable

I want to test which program is faster and consumes less time and memory.

It may be possible that the difference is very small but still I would like to know which is fast program.

Is their anyway using which, I can measure a performance of a simple and complex program.

Upvotes: 0

Views: 492

Answers (3)

Mohit Kanwar
Mohit Kanwar

Reputation: 3050

You may simply use System.currentTimeMillis() before and after the operation and find the difference.

However, If the time difference is too small, you may not be able to realize it. (As time taken during an operation is dependent upon various other factors.) You may want to run the same operation for a large number (e.g. 1 million times) using a loop and find the average time taken.

Or you may use some external profilers as stated by @wa11a above.

However do note that Static variables are loaded at the time the class is loaded, where as normal variables are loaded when required (after the class has loaded).

Hence Static variables would perform better, as they are already loaded with the class and stay there for long.

However, making a static variable has its own disadvantages. It is not extensible by using concepts of OOPs.

Static and default variables have their own usages. Use what is appropriate to your case.

Upvotes: 1

Xenonite
Xenonite

Reputation: 1965

How about you do a very simple and easy benchmark?

long start = System.currentTimeMillis();
// do your operation
System.out.println("operation took " + System.currentTimeMillis() - start + "milliseconds");

This outputs how long the operation took. Usually, if the operation is not that time intensive, it will not detect any difference. Hence, you should loop the operation you want to benchmark for like 10000 times.

Upvotes: 0

wa11a
wa11a

Reputation: 181

What you need is to use a profiler.

See here,

http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html

Good luck.

Upvotes: 1

Related Questions