When I read tutorials about java, I sometime's get to a place where they say that one method is faster than the other. Take for example the ++ x being a bit faster than the x ++.
How do you test to see which method is faster?
Printable View
When I read tutorials about java, I sometime's get to a place where they say that one method is faster than the other. Take for example the ++ x being a bit faster than the x ++.
How do you test to see which method is faster?
Simply use
Code:long tim = System.currentTimeMillis();
for ( i = 0; i < 30000; i++ ){
//System.out.println(++x);
// your statements
}
System.out.println( System.currentTimeMillis() - tim );
----
BTW, on my machine x++ took 641 millisecs, where as ++x took 609 millisecs.
MC: it is very easy to test: just use rjuyal's example as a template:
Your loop will have to very big for anything to register in the timer.Code:start timer (currentTimeMillis())
loop using ++x
end loop
stop timer (currentTimeMillis())
start timer (currentTimeMillis())
loop using x++
end loop
stop timer (currentTimeMillis())
Question: why are you asking questions about the speed between different operators ? Are you developing an application that is performance dependent?
Luck,
CJSL
just want to optimize all the applications I make , in the future
Unless your applications are performence dependant, do an extremely lot of intensive cycles or interface with the mechanical real world, I don't think you have to worry about which methods run faster then others. Please see the following post:
http://www.java-forums.org/new-java/...stigating.html
On my PC I was able to detect a 200ms difference between three methods after 50 million cycles and the results weren't definite.
You should probably worry more about the way the application is programmed (correct use of methods/OOP, instantiation, etc) than the methods themselves.
Luck,
CJSL