ANTS Performance Profiler - 7.4

# Worked example: Profiling performance of an algorithm - ANTS Performance Profiler

This worked example shows how you can use ANTS Performance Profiler to identify the most time-critical parts of a demonstration application, particularly where two different approaches to a problem are optimal in different circumstances.

Note that the demonstration application used in this worked example is supplied with ANTS Performance Profiler 6.3 and later. If you have an earlier version of ANTS Performance Profiler, either upgrade to version 6.3, or see the Mandelbrot worked example for version 6.2.

### Introducing TimeLineDemo

This worked example uses TimeLineDemo.

TimeLineDemo is a simple Windows application that checks which of a set of numbers are prime.

There are two different versions of TimeLineDemo:

• In Brute Force configuration, a brute-force method is used to check whether the number is prime.

The Brute Force build of TimeLineDemo is found in %Program Files%\Red Gate\ANTS Performance Profiler 7\Tutorials\CS\Precompiled\TimeLine\BruteForce\

• In Miller-Rabin configuration, the Miller-Rabin algorithm is used to check whether the number is prime.

The Miller-Rabin build of TimeLineDemo is found in %Program Files%\Red Gate\ANTS Performance Profiler 7\Tutorials\CS\Precompiled\TimeLine\MillerRabin\

The application has two options:

• Max Random
• Sample Size

When TimeLineDemo first starts, it creates a list of as many prime numbers as possible in 15 seconds.

When you click Go, TimeLineDemo creates a list of positive integers, which is Sample Size items long. All of the integers are random numbers between 1 and Max Random. For each integer in the list, TimeLineDemo checks whether the number is prime, using the following algorithm:

1. If the number is in the list created in the first 15 seconds, it is prime.
2. If a number in the list created in the first 15 seconds is a factor of the current number, the current number is not prime.
3. In all other cases, use either a brute-force mechanism or the Miller-Rabin algorithm to check whether the number is prime (depending on the build in use).

Note: Strictly speaking, the Miller-Rabin algorithm is probabilistic. For the purposes of this demonstration, however, the algorithm can be assumed to be deterministic. This is because the algorithm's results are exact for integers which are smaller than the largest integer that the field accepts.

### Walkthrough

1. Start ANTS Performance Profiler
2. Select .NET executable
3. Enter the path to the brute-force build of TimeLineDemo.exe
4. Click Start Profiling
5. If required by your configuration, grant elevation permission
6. The demo application starts and goes through initial (start-up) phase
7. The application is shown and processor use decreases
8. Click the ANTS Performance Profiler logo
9. Set MaxRandom to 500,000 and SampleSize to 500,000
10. Click OK
11. Click Go
12. The application performs the calculations
13. To see the expensive calculation methods, drag over that phase in the timeline
14. In the Call tree, select RedGate.Demo.MainWindow.<GoButtonClick>
15. The calculations took 1029 milliseconds; that's quick enough
16. Open the File menu and click New Profiling Session...
17. Click Start Profiling
18. When TimeLineDemo has started, click the ANTS Performance Profiler logo
19. Set MaxRandom to 2000000000 and SampleSize to 5000000
20. Click OK
21. Click Go and wait for the calculations to finish
22. Select the calculation phase in the timeline, then RedGate.Demo.MainWindow.<GoButtonClick>
23. The calculations took 175,252 milliseconds or nearly 3 minutes. That's too slow.
24. To see the brute-force method, open TimeLineDemo.csproj in Visual Studio
25. In APP, right-click `ResultSet.Generate()` and select Open with (Solution)

If prompted to elevate Visual Studio, click Ignore.

26. VS shows the expensive line. Right-click the method. Select Go to Declaration.
27. The Generate() method is displayed
28. The brute-force method is in use. Try the Miller-Rabin algorithm instead.
29. To switch to Miller-Rabin, click Debug and select Configuration Manager...
30. Next to TimeLineDemo, click Debug and select MillerRabin. Rebuild the demo.
31. In APP, open the File menu and click New Profiling session...
32. Change the .NET executable to the Miller-Rabin build
33. Click Start Profiling and wait for the demo to initialize
34. Click the ANTS Performance Profiler logo
35. Set MaxRandom to 2000000000 and SampleSize to 5000000
36. Click Go
37. Select the calculation phase on the timeline as before
38. The calculations took 66,149 milliseconds, about 66% faster than brute-force
39. Is Miller-Rabin always faster than brute-force? Profile TimeLineDemo again.
40. Set MaxRandom to 500000 and SampleSize to 500000 again
41. The calculations took 1,545 milliseconds, which is slower than brute-force

### Conclusion

This walkthrough has demonstrated a realistic programming scenario. It has compared two methods for testing if a number is prime and shown that Miller-Rabin is more efficient than the brute force approach when the number being tested is large.

This walkthrough has also demonstrated that you can select just a portion of the timeline when analyzing results, allowing you to ignore a slow initialization phase, for example.

### Learning more

To perform the task demonstrated in this walkthrough more efficiently, you can use the following additional functionality:

• To help you identify the section of the timeline to select, you can refer to the Event markers in the Events bar.
• You can name and bookmark sections of the timeline.