0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Prabhat Yadav
  • 1,181
  • 6
  • 18
  • 29
  • 2
    Possible [duplicate](http://stackoverflow.com/questions/2713940/eclipse-java-profiler). – npinti Aug 28 '15 at 08:36
  • The answer might depend on which JVM you use. Or if you're running client or server mode. Or if you mean interpreted mode or JIT'ed. And possibly which OS you're using. And which architecture you're running on. My recommendation: Think instead of whether it *makes sense* to make the variable static or not. – aioobe Aug 28 '15 at 08:42
  • @aioobe Thanks for your reply and time. I have a common OS(window 10) and Jvm(jdk 1.7.60) for both static variable and default variable. so i don't think provided input can effect performance. I really appreciate if you provide any notes link of book where i can find more about it. I have one more question. Is it compilation and processing time is depend (different for different) operating system. If the ans is yes then how java is platform independent ? – Prabhat Yadav Aug 31 '15 at 13:13
  • For these simple things, do the right thing instead. Static variables should only be used when you know why you need to! – Thorbjørn Ravn Andersen Aug 19 '17 at 12:38

3 Answers3

1

What you need is to use a profiler.

See here,

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

Good luck.

wa11a
  • 183
  • 1
  • 7
1

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.

Mohit Kanwar
  • 2,962
  • 7
  • 39
  • 59
  • 1
    Static variable are loaded at the time class load andstay their for long time it shows the static variable is good in processing time but it increase the compilation time and consume more memory for a long time rather than default variable. – Prabhat Yadav Aug 31 '15 at 13:04
0

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.

Xenonite
  • 1,823
  • 4
  • 26
  • 39
  • I don't think 10000 is enough to make sure the JVM is warmed up and the code snippet under test has been (JIT) compiled. Writing JVM micro benchmarks [is hard](http://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java). – aioobe Aug 28 '15 at 08:43
  • yep - i just assumed, that he wants to benchmark something like ´static int x = 5;´ against `int x = 5;` and not some complex calculations – Xenonite Aug 28 '15 at 08:45