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:
- If the number is in the list created in the first 15 seconds, it is prime.
- 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.
- 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
- Start ANTS Performance Profiler
- Select .NET executable
- Enter the path to the brute-force build of TimeLineDemo.exe
- Click Start Profiling
- If required by your configuration, grant elevation permission
- The demo application starts and goes through initial (start-up) phase
- The application is shown and processor use decreases
- Click the ANTS Performance Profiler logo
- Set MaxRandom to 500,000 and SampleSize to 500,000
- Click OK
- Click Go
- The application performs the calculations
- To see the expensive calculation methods, drag over that phase in the timeline
- In the Call tree, select RedGate.Demo.MainWindow.<GoButtonClick>
- The calculations took 1029 milliseconds; that's quick enough
- Open the File menu and click New Profiling Session...
- Click Start Profiling
- When TimeLineDemo has started, click the ANTS Performance Profiler logo
- Set MaxRandom to 2000000000 and SampleSize to 5000000
- Click OK
- Click Go and wait for the calculations to finish
- Select the calculation phase in the timeline, then RedGate.Demo.MainWindow.<GoButtonClick>
- The calculations took 175,252 milliseconds or nearly 3 minutes. That's too slow.
- To see the brute-force method, open TimeLineDemo.csproj in Visual Studio
- In APP, right-click
ResultSet.Generate()and select Open with (Solution)If prompted to elevate Visual Studio, click Ignore.
- VS shows the expensive line. Right-click the method. Select Go to Declaration.
- The Generate() method is displayed
- The brute-force method is in use. Try the Miller-Rabin algorithm instead.
- To switch to Miller-Rabin, click Debug and select Configuration Manager...
- Next to TimeLineDemo, click Debug and select MillerRabin. Rebuild the demo.
- In APP, open the File menu and click New Profiling session...
- Change the .NET executable to the Miller-Rabin build
- Click Start Profiling and wait for the demo to initialize
- Click the ANTS Performance Profiler logo
- Set MaxRandom to 2000000000 and SampleSize to 5000000
- Click Go
- Select the calculation phase on the timeline as before
- The calculations took 66,149 milliseconds, about 66% faster than brute-force
- Is Miller-Rabin always faster than brute-force? Profile TimeLineDemo again.
- Set MaxRandom to 500000 and SampleSize to 500000 again
- 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.
For more information, see Working with the timeline.
Was this article helpful?
ANTS Performance Profiler
- Installation error 5100 when installing ANTS Performance Profiler and ANTS Memory Profiler
- Attach to process unavailable with some anti-virus software
- Memory leaks observed when profiling Windows Presentation Framework (WPF) applications
- Log files
- The manifest for the SQL server event source did not match the generated events. Some SQL events may report incorrect data. To fix this issue, consider recompiling etwcls.mof
- Setting file I/O and child process profiling in high DPI modes
- HTTP request timings in IIS
- Profiling web services in IIS Express
- Method not found: 'UInt32 <Module>._ANTS_Begin_Sql(System.String)'
- Enabling line-level timings for SecurityTransparent code
- Profiling SharePoint 2010 in v6.0 - 6.3
- Profiling SharePoint subsites
- Problems synchronizing results
- Windows service profiling fails if the service uses a system account
- Profiling assemblies protected with DeployLx
- The type initializer for 'y.layout.hierarchic.ClassicLayerSequencer' threw an exception
- Forcing your application to use .NET 4
- Double hit counts occurring on one line
- System.EntryPointNotFoundException (Versions: 3,4 only)
- Methods in (ASCX) web controls may not appear
- Failed to CoCreate Profiler error profiling a Windows service
- Multiple-core portable computers may show inflated times (version 3)
- No .NET methods were profiled on web application
- ANTS Profiler code instrumentation method
- Add-in fails to load when using RunAs to start Visual Studio as another user
- Profiling unit tests using Nunit
- Can I profile Compact Framework applications?
- Profiling a SharePoint 2007 Web Part
- Profiling an assembly in the Global Assembly Cache (GAC)
- Profiling IIS web applications that need to run under the SYSTEM account
- Profiling a web application hosted in IIS on a fixed TCP port
- Profiling client-side XBAP .NET applications
- ANTS Profiler prompts for location of source code which is not your own source code
- ANTS menu remains in Visual Studio after uninstall of ANTS Profiler
- Profiling a web application needing to run as the Network Service account
- Cannot start COM+ application via Remote Desktop
- Finding the overall time taken to execute a thread
- Profiling BizTalk applications
- Profiling Microsoft Office managed-code add-ins
- The system cannot find the file specified
- Error Stopping IISAdmin profiling IIS web application on Windows XP
- Creating a global debugging symbols (PDB) directory
- Using the add-in to profile solutions containing more than one project
- Methods may be missing on multi-core systems in version 3
- ASP .NET 1.x application recycling causing incomplete results
- Application recycling causes incomplete results and other errors when profiling ASP .NET
- (version 5) Support for profiling Silverlight applications
- Couldn't open metabase error when profiling ASP .NET hosted in IIS
- Failed to coCreate Profiler on ASP .NET web application
- HTTP error 404.17 profiling ASP .NET 1.1 on IIS 7
- Performance counters missing from results
- Isolating single ASP .NET pages in ANTS Profiler results
- Missing hits for lines in the source code view
- ANTS Profiler restarts IIS
- Showing the amount of time taken for a method in one particular thread
- Please specify a valid URL message profiling ASP .NET
- Times on individual lines do not add up to method time
- Explanation of "thread blocked" methods in ANTS Profiler
- Call graph percentages do not add up exactly
- Operation could destabilize the runtime error profiling ASP .NET
- IIS ceases to work after profiling web applications
- Problems with ANTS Visual Studio integration
- ANTS Profiler has been split into two distinct programs
- Windows Installer errors 1603 and 2738 occurring during installation
- Times in source code window are greater than the times showing in the method grid or tree view
- Problems with ANTS Performance Profiler Visual Studio integration
- Profiling ClickOnce applications deployed to IIS
- Could not enable tracing due to error code 112
- AjaxControlToolkit incompatibility causing application being profiled to crash
- Could not enable tracing because SQL Express is installed
- ANTS Performance Profiler menu items not showing in Visual Studio 2010
- Profiler stopping while profiling an in-browser Silverlight application
- "No Disk" error occurring while profiling application
- IE error "Internet Explorer cannot display the webpage" when profiling a website
- Profiler failing to connect to the server when profiling web applications
all products
- Some Red Gate products identified as containing a trojan by Anti-Virus software
- Activation may fail with Unknown Error -1
- Product uses web help although a CHM file is available locally
- Argument exception resulting from missing environment variable
- Check for updates may fail when used through proxies
- 'Unidentified Publisher' error when repairing or uninstalling
- Licensing activates product as standard edition
- Moving Red Gate software products to another machine
- Red Gate tools log locations
- The application UI opening slowly when there is no internet access
ANTS Performance Profiler
- Activating your products
- Activating your products
- Interpreting unexpected profiling results
- Choosing a profiling mode in ANTS Performance Profiler 6
- ANTS Performance Profiler 7 release notes - version 7.xx
all products
- Red Gate product acknowledgements
- Activating your products
- Activating your products
- Red Gate bundle history
- Check for updates
- Troubleshooting Check for Updates errors
- Current versions
- Deactivating your products
- Installing Red Gate products from the .msi file
- Requesting additional activations
- Serial numbers for bundles
- Reactivating using a different serial number
- Extending your trial
- Finding your serial numbers
- Moving a serial number from one computer to another
- No response received for manual activation
- Licensing and activation resources
- Licensing and activation resources
- Troubleshooting licensing and activation errors
- Licensing and activation FAQs
- Red Gate tools log file locations
- Download old versions of products
- Download product prerequisites & utilities
- Support & upgrades
- Upgrading your software
- Upgrading FAQs
ANTS Performance Profiler
- Worked example: Profiling an ASP.NET application - ANTS Performance Profiler
- Worked example: Profiling from the command line - ANTS Performance Profiler
- Worked example: Profiling performance of an algorithm - ANTS Performance Profiler
- Worked example: Profiling network overheads - ANTS Performance Profiler

Using ANTS Performance Profiler