• Post Reply Bookmark Topic Watch Topic
  • New Topic

[Newbie] Measuring code execution performance

 
Jason Barker
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have two different ways of performing a function and I want to measure the efficiency of each. I'm thinking of just measuring the elapsed time for each method, comparing them and then choosing the shorter time. Would anyone suggest alternative methods of testing performance?

Thanks,
Jason
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason,
Your solution is a good one. One thing I would add to it is to run the function in a loop - say a few thousand times. It will give you more accurate numbers.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing to notice: Microbenchmaking - benchmarking a short amount of code outside its context - has a high risk of being misleading. Even if you find that one solution seems to be a hundred times faster than the other, it still might no noticeable difference at all in the context of an application.

Typically a better solution is too first optimize code for maintainability, then observe wether you've a performance problem and use a profiler to find the bottleneck(s).
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And repeated tests in different order is a must.

Maybee the system is doing some index-updating in the background, while running the one implementation.

For small differences, running millions of tests might be neccessary.
Try to run it, till the difference is at least 1 second.

Run it wiht different numbers of iterations - one algorithm might need
100000 * N * x, the other
N*N * x
so depending on the number of iterations (N) once the first algo is better, once the second.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JAMON toolkit is said to be very handy for this sort of thing.
Bill
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jason
I have interested to analysis your program and give some opinion on which one is better, through discussion, we can find out which one is better. As I would like to compare the analysis that whether it can use in the real live or not. :roll: I wonder if you could write some psudo-code here for two different ways of performing a function.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!