Clear buffers for C# performance test?

Go To


In SQL Server when I want to check performance, I clear all buffers - in order to get true result : I use


and then I check differentials between GetDate()'s

What command should I use in C# in order to clear any buffer/"speed helper" mechanism ? (I'm intending to use stop watch).


2012-04-03 20:01
by Royi Namir
This depends greatly on what your code does. Can you provide more details? Also, in general, performance measurement should be done after any time-consuming initialization, and after the algorithm has run for long enough to settle into a steady state - jnylen 2012-04-03 20:04
@jnylen I dont care about code. I care about preventing cache from being considered at the second time. I want all >1 times to be as much as the same as the first execution. I need to clear buffer. how - Royi Namir 2012-04-03 20:06
Royi Namir: What cache? What buffer? There is no "C# code buffer/speed helper" that encompasses all potential algorithms, which is why I asked for clarification. As zmbq mentions, the low-level constructs such as the compiler and the CPU caches are important to real-world performance and should be measured in your benchmark - jnylen 2012-04-03 20:18
To measure the performance of C# code, you have to "care about" the code and what it is doing. Otherwise your values will most likely be meaningless - jnylen 2012-04-03 20:19


A .NET program has a lot of 'caches', and not all of them can be cleared. You have the CPU caches that are filled not only by your process, but also by other processes running on your computer. You have the jitter that compiles your functions the first time they're called, making the first iteration a lot longer than any other iteration. If you're using any sort of I/O, you have those caches and buffers as well.

Besides, why would you want to clear the caches? They are there to help you. In the real world, a cache hit will help your performance - why not measure your program's performance while taking caches and buffers into account?

Anyway, I think you should use a profiler instead of a Stopwatch to measure your performance. That goes for SQL Server as well.

2012-04-03 20:14
by zmbq