John Lam 1997 — 2026

Measuring JIT Cost

Recently on the DOTNET list, there was much speculation as to the cost of JIT compilation. There was also a lot of speculation as to whether the JIT compiler actually cached the x86 code that it generated (it does not). This led to a lot of discussion about alternate implementations of JIT compilers that could do things like cache the result of their work.

Because of my background from my other life, I had to see what the bottom line was on the cost of JIT compilation. I carved out a chunk of time this afternoon to experiment with measuring the cost of JIT compilation. I wrote a simple utility called JitCost that uses the CLR profiling interfaces to measure the amount of time that was spent inside of the JIT compiler.

Here are the raw results of five separate runs of WinCV (the WinForms Class Viewer that can be found in the FrameworkSDK\bin directory):

| Method | 1 | 2 | 3 | 4 | 5 |

| 9459128 | 11946 | 29774 | 12168 | 11930 | 11965 |

| 9461304 | 548 | 391 | 410 | 383 | 657 |

| 9458856 | 30726 | 12674 | 12885 | 30725 | 32555 |

| 9458872 | 25175 | 27104 | 25507 | 25035 | 24883 |

| 9459112 | 54307 | 39953 | 38694 | 39781 | 38536 |

| 9460936 | 1771 | 1750 | 1780 | 1782 | 1769 |

| 9460920 | 921 | 900 | 912 | 921 | 919 |

| 9458920 | 5967 | 5948 | 5973 | 5970 | 5952 |

| 9464136 | 594 | 1094 | 584 | 604 | 592 |

| 9464120 | 12756 | 12605 | 12747 | 14731 | 12576 |

| 9459080 | 894 | 903 | 919 | 923 | 901 |

| 9458904 | 14899 | 14983 | 16565 | 14936 | 14957 |

| 9458888 | 9901 | 9889 | 9986 | 9868 | 11495 |

| 9459000 | 716 | 802 | 722 | 815 | 733 |

| 9458936 | 13498 | 11289 | 11268 | 11266 | 11098 |

| JIT (ticks) | 184619 | 170059 | 151120 | 169670 | 169588 |

| JIT (s) | 5.16E-02 | 4.75E-02 | 4.22E-02 | 4.74E-02 | 4.74E-02 |

The average time spent inside of the JIT was 0.047 +/- 0.003 seconds.

The raw numbers represent ticks of the high resolution counter. On my computer (a ThinkPad T23 running a 1.13GHz Pentium III-M processor), the high-resolution counter ticks roughly 3.58 million times per second. As you can see, these numbers are reasonably reproducible. Of course, on a computer that runs a multi-tasking operating system, getting reproducible numbers is fairly difficult, especially under conditions where you are trying to measure very small time increments.

The thing that surprised me the most was the relatively small number of functions (15) that must be JIT compiled before the main window of a Win Forms application appears. Clearly there are tremendous benefits on start-up time for having pre-compiled x86 binaries for the runtime libraries. It would be interesting to compare the results of having to JIT compile every method vs. only the user-defined methods for WinCV. I’ll leave that as an experiment for another day.

The worst case compilation time was for method 9459112, and that took approximately 0.01 seconds, or nearly 20% of the total amount of time spent JIT compiling code in WinCV. It would be interesting to see what those methods were, how many MSIL instructions were compiled, and how many x86 instructions were generated. While not incredibly useful, it would be fun to see what the throughput of the JIT compiler is in terms of CIL instructions / second.

Using JitCost is fairly simple. You will need to download the binary bits and register measure.dll using regsvr32. You will also need a debug console viewing application such as DBGVIEW from SysInternals. All applications whose JIT cost that you wish to measure must be started from a command console. Before you can measure, you must first run the enablelistener.cmd script in your console window to set the COR_PROFILER environment variable to the CLSID of the JitCost COM object. DBGVIEW will show you the results in a format that resembles:

[1984] Frequency of high-performance counter: 3579545 [1984] Time to compile method: 9459128 = 12292 ticks [1984] Time to compile method: 9461304 = 394 ticks [1984] Time to compile method: 9458856 = 12892 ticks [1984] Time to compile method: 9458872 = 25642 ticks [1984] Time to compile method: 9459112 = 38794 ticks [1984] Time to compile method: 9460936 = 1769 ticks [1984] Time to compile method: 9460920 = 902 ticks [1984] Time to compile method: 9458920 = 6049 ticks [1984] Time to compile method: 9464136 = 590 ticks [1984] Time to compile method: 9464120 = 12783 ticks [1984] Time to compile method: 9459080 = 904 ticks [1984] Time to compile method: 9458904 = 15082 ticks [1984] Time to compile method: 9458888 = 9948 ticks [1984] Time to compile method: 9459000 = 742 ticks [1984] Time to compile method: 9458936 = 11075 ticks [1984] Total elapsed time in JIT: 4.186510e-002 seconds

Enjoy! Please send comments / flames / suggestions to the usual place.

Download the binary redistributable bits here.

Download the source code here.

Update: I just recompiled the bits today (February 4, 2002) using the RTM (Build 3705) Build of the CLR. The binary redistributable has been updated with the new compiled DLL. The source code file is unchanged.