ANTS Performance Profiler 11

Worked example - Profiling performance of an algorithm

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.

Aims of the worked example

This worked example demonstrates a realistic programming scenario: it compares two methods for testing if a number is prime, to find out which is more efficient when the number being tested is large.

It also shows that you can select just a portion of the timeline when analyzing results, for example so you can ignore a slow initialization phase.

Introducing the demo application

This worked example uses the TimeLineDemo, a simple Windows application that checks which of a set of numbers are prime.

There are two different versions of TimeLineDemoThe Brute Force configuration uses a brute force to check if a number is prime. The Miller-Rabin configuration uses the Miller-Rabin algorithm to check if a number is prime.

TimeLineDemo has two inputs:

  • Max Random
  • Sample Size

When TimeLineDemo starts, it generates prime numbers for 15 seconds, which gives it a slow initiialization phase. Then, when you click Go, it creates Sample Size positive integers, between 1 and Max Random. For each integer, TimeLineDemo checks if 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 if the number is prime.

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

We're going to start by seeing how the Brute Force build does with relatively small numbers as input.

  1. Start ANTS Performance Profiler and click New profiling session.
  2. From the list on the left, select .NET executable.
  3. Enter the path to the brute force build of TimeLineDemo.exe 
    This is %Program Files%\Red Gate\ANTS Performance Profiler 8\Tutorials\CS\Precompiled\TimeLine\BruteForce\ 
  4. Click Start profiling.
     
  5. If an elevation permission dialog opens, grant permission.
    TimeLineDemo starts and goes through the initialization phase.
    Once that's finished, you can see on the timeline that processor use drops to almost nothing.
  6. In TimeLineDemo, click the ANTS Performance Profiler logo to open the Configuration dialog.
  7. We're going to start with fairly small numbers. Set MaxRandom to 500000 and SampleSize to 500000:

    Click OK.
  8. In the main application window, click Go.
    TimeLineDemo performs the calculations.
  9. To see the expensive calculation methods, select that phase in the timeline.
  10. On the right-hand side under the timeline, next to Timing options, select Milliseconds as the unit of measurement.
  11. In the Call tree, select RedGate.Demo.MainWindow.<GoButtonClick>
  12. The calculations didn't take long - in the screenshot, it's 1121 milliseconds.

Now we'll see how the Brute Force configuration deals with larger numbers.

  1. On the File menu, click New Profiling Session.
    If a warning dialog comes up, click Continue Anyway
  2. On the main ANTS Performance Profiler page, click Start Profiling
  3. When TimeLineDemo has started, click the ANTS Performance Profiler logo to open the Configuration dialog.
  4. Now we're going to try much larger numbers. Set MaxRandom to 2000000000 and SampleSize to 5000000, then click OK.

     An advance warning: this could take quite a long time!

  5. In the main application window, click Go.
    TimeLineDemo performs the calculations. 
  6. Select the calculation phase in the timeline, then, in the call tree, look at RedGate.Demo.MainWindow.<GoButtonClick>
  7. This time, the calculations took much longer - in the screenshot, it's 182,987 milliseconds or over 3 minutes.
  8. Looking further down the call tree, the IsPrime() method is being called a very large number of times.
  9. We can take a look at the method that's taking so long. Right-click a method you're interested in, and select Open with new instance of Visual Studio.
    Visual Studio opens, showing the line of code that's taking so long.
  10. If you're interested in investigating the source code further, you can open it from %Program Files%\Red Gate\ANTS Performance Profiler 8\Tutorials\CS\Demos\TimeLineDemo\TimeLineDemo.csproj

Now we've tried the brute force method, let's see if using the Miller-Rabin algorithm works any better.

  1. In ANTS Performance Profiler, on the File menu, click New Profiling Session.
    If a warning dialog comes up, click Continue Anyway
  2. On the main ANTS Performance Profiler page, click New profiling session.
  3. Change the .NET executable to the Miller-Rabin build of TimeLineDemo.exe.
    This is found in %Program Files%\Red Gate\ANTS Performance Profiler 8\Tutorials\CS\Precompiled\TimeLine\MillerRabin\ 
  4. Click Start Profiling.
    If an elevation permission dialog opens, grant permission.
  5. TimeLineDemo starts and goes through the initialization phase.
  6. In TimeLineDemo, click the ANTS Performance Profiler logo to open the Configuration dialog.
  7. Set MaxRandom to 2000000000 and SampleSize to 5000000, then click OK.
  8. In the main application window, click Go.
  9. Select the calculation phase on the timeline, as you did earlier.
  10. It looks like the Miller-Rabin algorithm is much faster - in this screenshot, the calculations took 64,071 milliseconds, about 65% faster than brute force

So for large numbers, Miller-Rabin is faster than brute force. But is true for smaller numbers too?

  1. Profile TimeLineDemo again, but this time, set MaxRandom to 500000 and SampleSize to 500000 - the same as the first time we profiled the brute force algorithm.
  2. These calculations took 2,232 milliseconds - almost twice as long as it took the brute force algorithm. So for smaller numbers, Miller-Rabin is slower than brute force.

Learning more

To perform the task in this worked example more efficiently, you can use the following:

  • 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.

For more information, see The timeline.


Didn't find what you were looking for?