Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to measure performance speed of the method  RSS feed

 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class like this:




Then in another class where my main method is i have tried to use this


Now what i want to do is to measure the performance of my first method here. Is there any way to measure how fast my algorithm is?

The problem here is that my code is executed but i do not see in my console the time it takes. Am i doing it wrong?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 37261
519
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From a design point of view, you should run the code in a loop and measure the average performance of a lot of runs. The reason is that Java optimizes so the first run isn't representative.

As far as not seeing anything on the console, try adding System.out.println("Before"); and System.out.println("After"); before and after. Do those get output? How are you running the code? Command line?
 
barlet south
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No i am using Eclipse. Could you please tell me how to put a for loop in that piece of code in order to get the average?
 
Henry Wong
author
Sheriff
Posts: 23284
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:From a design point of view, you should run the code in a loop and measure the average performance of a lot of runs. The reason is that Java optimizes so the first run isn't representative.


I would argue that a large number of the initial iterations should be simply ignored. This way, the just-in-time compiler can do its job, and you won't be measuring the JIT compiler. Also, while not a good idea for applications, it may be needed for benchmarks to force the GC. This way, you won't be measuring the garbage collector too.

Henry
 
Stephan van Hulst
Saloon Keeper
Posts: 7821
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't it best to just use tools that specialize in benchmarking? I think most IDEs come with a profiler that can give you detailed information about performance.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!