Skip to content, Skip to search


Debugging Exercises

291 bytes added, 07:56, 17 December 2015
Exercise 8: Profiling
==Exercise 8: Profiling==
* Identifying bottleneck functions in a code segmentIdentify performance bottlenecks
In the other debugging exercises, we look at programs failing due to errors. Another common concern is program ''performance'' - does your program run in a reasonable time frame? This often can not be deduced by simply looking at the code. So the first step in fixing performance is identifying the location of the slowdown.
Debugging often involves finding the function which performs poorly - in terms of computation time and/or memory resources used. For the code to perform as desired , fixing the poorly performing function may often lead to a simple solution.  Exercise 8 in pretty straightforward with the main function calling two functions - <code>doStuff() </code> and <code>doMoreStuff() </code>. Both functions are called one after the other for two minutes. Our goal is to find how much time which function is taken by each function in slower than the two minute runother. One solution a programmer may think of is to keep track of time taken by functions in some flag variables using comparing values returned by [ System time.currentTimeMillis], but this method does not scale up for many function callslarge program and requires modifications to the code. Since the tutorial It is for "debugging", we will not making any changes much more efficient and useful to code but will be using use a profiling tool profiler to monitor time spent in methods - Jvisualvm which is part of the [] which is a bare bones profiler with basic functionalities and is freely available(Debugging_Exercises#Requirements) troubleshooting tool.
'''Steps for exercise'''
# Insert a breakpoint on the <code>while</code> statement in main method
# Launch JvisualVM. All running java Java applications are shown in the in Applications Tab (left margin) of JvisualVM. Double Click to select E8PerceivingPerformance. Go to the profiler tab and click the CPU option. '''Note''' # Click on the settings checkbox on upper right corner of Jvisualvm. In CPU settings , make sure the class being profiled is '''<code>net.imagej.trouble.** </code>''' instead of <code>net.imagej.trouble.visible.** - </code>, or the profiler won't be looking in the right place [[File:E8Settings.png|center|thumb|400px|Adjust settings]]
# Switch to Eclipse and resume the execution of code
# Wait for the stipulated time , twiddle your thumbs. Or you can switch to JvisualVM and see how much time is taken by each of function in real time.
|Are the number of invocations/function calls of both functions same ?
|Yes. This number will change based on the length of profiling, but in this case we see both methods were called 83,573,448 times- so the difference in timing is truly due to length of method execution.
* Using a profiler tool when you need to identify Profiling tools allow quick and precise identification of performance bottlenecks. 
==Exercise 9: Multiple threads==
Bureaucrat, emailconfirmed, incoming, administrator, uploaders