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.

4
  • 2
    Possible duplicate. Commented Aug 28, 2015 at 8: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. Commented Aug 28, 2015 at 8: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 ? Commented Aug 31, 2015 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! Commented Aug 19, 2017 at 12:38

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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.

2 Comments

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.
yep - i just assumed, that he wants to benchmark something like ´static int x = 5;´ against int x = 5; and not some complex calculations

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.