• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

check efficiency

 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 265
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try System.currentTimeMillis()
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)!
 
Andrew Lit
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you partners
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic