0

Hi I want to measure time of method and store this value then the next time if the method will changed i check this value with the old value to see if the modification is better.

But the problem is without modification if i run the method several time , i get different values so then i can't make good benchmark.

What is a good measurement to have the same value for a method and like that if i compare it with it self several times, it will give me the same value otherwise it will give me a different value ?

I already use StopWatch, PerformanceCounter, UserProcessTime but all time have different value so maybe time is not good metric for making the bench.

Omega
  • 1,539
  • 1
  • 11
  • 18
  • 3
    I believe Stopwatch class is what you need: https://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch%28v=vs.110%29.aspx – George Findulov Apr 21 '15 at 10:53
  • 4
    You wil *always* get different values. You need to collect multiple measurements and average them. Better yet, use a performance profiler like the one in Visual Studio. It can measure the time taken to execute a specific function without interference from other processes. – Panagiotis Kanavos Apr 21 '15 at 10:53
  • @GeorgeFindulov: _"I already use StopWatch, PerformanceCounter, UserProcessTime but all time have different value"_ – Tim Schmelter Apr 21 '15 at 10:53
  • Already did all solution as i put on the question but without reaching the goal. can't make bench – Omega Apr 21 '15 at 10:54
  • @Omega: show how and what you have measured, also, what is your goal? Do you expect the same value in ticks? – Tim Schmelter Apr 21 '15 at 10:55
  • @TimSchmelter here is the goal What is a good measurement to have the same value for a method and like that if i compare it with it self several times, it will give me the same value otherwise it will give me a different value ? And i use stopWatch,PerformanceCounter,UserProcessTime before and after a method and take a difference. So i lunched this method several time to making sampling and get average or minvalue but can't reach the goal because i have always different value so i can't make a bench of the method. – Omega Apr 21 '15 at 10:57
  • 1
    Sorry, it doesn't make sense. Don't expect the same value in terms of ticks. Windows is not a real time operating system. There are many things which are not related to your method that have to be done while your program is running. – Tim Schmelter Apr 21 '15 at 11:00
  • I'll not have same value in ticks if we can it will be great. But maybe measure time to make bench for a method if his content change, is not good idea. Maybe their is other method that i didn't know which is not depending on time. – Omega Apr 21 '15 at 11:00
  • 1
    @Omega: as mentioned, you haven't shown what you are measuring, maybe that method causes memory pressure which forces the garbage collector to jump in. Or you're measuring IO or database stuff which i would never expect to be the same. – Tim Schmelter Apr 21 '15 at 11:02
  • 1
    You'll get different results at least the second and consecutive times because the JIT optimizes the call for subsequent execution of the same method. – aevitas Apr 21 '15 at 11:03
  • @TimSchmelter Just simple function to find primeNumber or to count odd element in table. int a = Time(); methodToCheck(); int b = Time(); – Omega Apr 21 '15 at 11:03
  • 1
    @Omega: then edit your question and add that stuff. What you call simple might be complex for the operating system. – Tim Schmelter Apr 21 '15 at 11:04
  • @aevitas yes that's why i thought that making bench on function with using time measurement is bad idea maybe i have to use other method – Omega Apr 21 '15 at 11:05
  • @Omega There's a whole myriad of performance analysis tools available that can help you with this. Take a look at RedGate's and JetBrains' for example. – aevitas Apr 21 '15 at 11:07

0 Answers0