TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# ProfileOptimization Example

This C# article uses the ProfileOptimization class. It shows syntax and benchmarks profiling, which improve startup speed.

ProfileOptimization helps programs start up faster.

We call the SetProfileRoot and StartProfile methods at startup. After the program exists, a profile file is created. At next startup, this profile is used to compile methods.

Note: ProfileOptimization requires a computer with at least two logical processor cores.

Note 2: If a single-core computer runs a ProfileOptimization program, the methods have no effect. They are silently ignored.

Based on:

.NET 4.5

Example. This program reveals the syntax of ProfileOptimization. Please add the System.Runtime with a using-directive. The SetProfileRoot method is called to indicate the folder where profiles are stored.

And: The StartProfile method specifies both the name of the profile file, and that profiling should begin.

Caution: To end profiling, the program must be exited normally—it cannot just be terminated.

C# program that uses ProfileOptimization

using System.Runtime;

class Program
{
    static void Main()
    {
	// Profiles directory must exist.
	// ... "profile" is created.
	ProfileOptimization.SetProfileRoot("C:\\profiles\\");
	ProfileOptimization.StartProfile("profile");
    }
}

Discussion. I did not design the ProfileOptimization feature, so I can't describe its implementation in depth. But the general concept is that the profile contains a list of the methods that are required by the program on execution.

Then: When a profiled program starts up, the .NET Framework reads in the profile. It then compiles those methods on a separate thread.

And: This relieves the pause due to JIT-compilation for those needed methods. This reduces startup time.

JIT

Stop profiling. One problem I ran into when using ProfileOptimization is that a program must be closed normally for the profile to be written. If you just close a console window, the profile is not written.

Performance. A performance optimization that does not optimize performance is worthless. For small or fast-running programs, ProfileOptimization does not help much. But on certain programs, it is worthwhile.

Actual results. I have a program that requires about 1 second to run. It has about 30,000 lines of C# code. Most of its runtime is disk access and reads. But I enabled ProfileOptimization for a significant benefit.

Results: The program runs approximately 10% faster with a profile than without a profile.

ProfileOptimization results

Before: 1074 ms
	1078 ms
	1076 ms
After:   964 ms
	 962 ms
	 961 ms

Summary. Profiling a program improves startup speed. It works only on computers with multiple processor cores. The feature requires a call to ProfileOptimization.SetProfileRoot and StartProfile. We saw real performance results.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf