aspose file tools*
The moose likes Beginning Java and the fly likes check efficiency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "check efficiency" Watch "check efficiency" New topic
Author

check efficiency

Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
Hallo,
I have one more question to be asked. I wonder how to check which algorithm is more efficient.
The only thing i thought of is getting the system time at the start of the program and then at the end.
Probably you could suggest something better?
By the way, how could i get system time
thank you in advance.
Chad McGowan
Ranch Hand

Joined: May 10, 2001
Posts: 265
try System.currentTimeMillis()
Dave Landers
Ranch Hand

Joined: Jul 24, 2002
Posts: 401
If, by "efficient", you mean "faster" then the best way generally is to use System.currentTimeMillis() to grab a time stamp before and after the code runs.
To make this accurate, you will have to be sure that the machine you are running it on is not busy doing anything else (other users, background tasks, etc).
If the program runs quickly, then you may have to run it in a loop and time several hundred or thousands of invocations to get useful numbers.
Also, you probably should run the test several times to average out interactions with the OS, disk access, memory, etc. For example, I have noticed recent versions of Windoze take a long time to load code if you have not run that program in a while, but is much faster the second time (must cache loaded libraries or something). So run things several times to "converge" on a useful number and also to average out system background tasks.
And running your program like this is probably not representative of what will really happen in a "real system", so you have to consider that.
For example, one program might run really fast but use lots of memory. It will look slow if you don't set up the JVM to give it enough heap (or you don't have enough physical memory on that machine) so it can show off its strengths.
Or maybe one program usually runs really fast, but the very first run takes a long time in order to set things up. Depending on how you test it, you might always be testing the first run's setup rather than the second, third, etc. execution speed.
I feel that these sorts of tests are mostly useful for trying to improve a program, rather than trying to set up a competition between two programs. Set a baseline, and see how it runs. Then you can start trying out improvements and see if it is better or worse (and which ones are a lot better).
Cheers,
Dave
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Dave Landers:
Also, you probably should run the test several times to average out interactions with the OS, disk access, memory, etc. For example, I have noticed recent versions of Windoze take a long time to load code if you have not run that program in a while, but is much faster the second time (must cache loaded libraries or something). So run things several times to "converge" on a useful number and also to average out system background tasks.

Another factor coming into play here is the Hotspot Engine of the JVM. It compiles the java byte code incrementally to native machine code while the program runs, making it faster and faster. This can improve performance by orders of magnitude - I once experienced an XSLT transformation using Xalan taking two seconds for the first time and only a tenth of a second at the fifth time (always the same file, of course)!


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Andrew Lit
Ranch Hand

Joined: Jul 01, 2002
Posts: 135
thank you partners
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: check efficiency