B
B
barkalov2012-07-17 02:41:38
.NET
barkalov, 2012-07-17 02:41:38

How to measure code performance directly in code?

Stopwatch won't work.
It is necessary to measure processor cycles (CPU-sampling or something like that). In code.
What for? I am writing a genetic algorithm that selects the optimal parameters for some complex method . The speed of this complex method is highly dependent on combinations of parameters. Therefore, speed is one of the criteria by which the fitness function selects the best candidates.
But the algorithm will work for about a week on a working machine, in the background, under various conditions. The processor is either free or busy, the same with the disk, so time measurements (via Stopwatch) under different conditions will simply be incomparable.
Some kind of effective time metric is neededspent by the application. Or something like CPU-sampling from VS Performance Profiler. But only right in the code. Like Stopwatch.
Or does it not happen?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LightSUN, 2012-07-17
@barkalov

This looks like it would work:

[ DllImport ( "kernel32.dll" ) ]
[ return :  MarshalAs ( UnmanagedType. Bool ) ]
static  extern  bool  QueryProcessCycleTime ( IntPtr ProcessHandle,  out  ulong  CycleTime ) ;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question