CsCheck/Check.cs
/// <summary>Assert the first Action is faster than the second to a given sigma.</summary>
/// <param name="faster">The presumed faster code to test.</param>
/// <param name="slower">The presumed slower code to test.</param>
/// <param name="sigma">The sigma is the number of standard deviations from the null hypothosis (default 6).</param>
/// <param name="threads">The number of threads to run the code on (default number logical CPUs).</param>
/// <param name="repeat">The number of times to call each of the actions in each iteration if they are too quick to accurately measure (default 1).</param>
/// <param name="timeout">The number of seconds to wait before timing out (default 60). </param>
/// <param name="raiseexception">If set an exception will be raised with statistics if slower is actually the fastest (default true).</param>
Assert the first Action is faster than the second to a given sigma.
Assert the first Action is faster than the second to a given sigma .
public static FasterResult Faster(Action faster, Action slower,
            double sigma = -1.0, int threads = -1, int repeat = 1, int timeout = 60, bool raiseexception = true)
        {
            if (sigma == -1.0) sigma = Sigma;
            sigma *= sigma;
            if (threads == -1) threads = Threads;
            if (threads == -1) threads = Environment.ProcessorCount;
            var r = new FasterResult { Median = new MedianEstimator() };
            var mre = new ManualResetEventSlim();
            Exception exception = null;
            while (threads-- > 0)
                ThreadPool.UnsafeQueueUserWorkItem(_ =>
                {
                    try
                    {
                        while (!mre.IsSet)
                        {
                            long tf = 0L, ts = 0L, st = 0L;
                            for (int i = 1; i < repeat; i++)
                            {
                                st = Stopwatch.GetTimestamp();
                                faster();
                                tf += Stopwatch.GetTimestamp() - st;
                                st = Stopwatch.GetTimestamp();
                                slower();
                                ts += Stopwatch.GetTimestamp() - st;
                                if (mre.IsSet) return;
                            }
                            st = Stopwatch.GetTimestamp();
                            faster();
                            tf += Stopwatch.GetTimestamp() - st;
                            st = Stopwatch.GetTimestamp();
                            slower();
                            ts += Stopwatch.GetTimestamp() - st;
                            if (mre.IsSet) return;
                            r.Add(tf, ts);
                            if (r.SigmaSquared >= sigma) mre.Set();
                        }
                    }
                    catch (Exception e)
                    {
                        exception = e;
                        mre.Set();
                    }
                }, null);
            bool completed = mre.Wait(timeout * 1000);
            if (raiseexception)
            {
                if (!completed) throw new CsCheckException("Timeout! " + r.ToString());
                if (exception is not null || r.Slower > r.Faster || r.Median.Median < 0.0) throw exception ?? new CsCheckException(r.ToString());
            }
            return r;
        }
public static FasterResult Faster ( Action faster , Action slower , double sigma = - 1.0 , int threads = - 1 , int repeat = 1 , int timeout = 60 , bool raiseexception = true ) { if ( sigma == - 1.0 ) sigma = Sigma ; sigma *= sigma ; if ( threads == - 1 ) threads = Threads ; if ( threads == - 1 ) threads = Environment . ProcessorCount ; var r = new FasterResult { Median = new MedianEstimator ( ) } ; var mre = new ManualResetEventSlim ( ) ; Exception exception = null ; while ( threads -- > 0 ) ThreadPool . UnsafeQueueUserWorkItem ( _ => { try { while ( ! mre . IsSet ) { long tf = 0L , ts = 0L , st = 0L ; for ( int i = 1 ; i < repeat ; i ++ ) { st = Stopwatch . GetTimestamp ( ) ; faster ( ) ; tf += Stopwatch . GetTimestamp ( ) - st ; st = Stopwatch . GetTimestamp ( ) ; slower ( ) ; ts += Stopwatch . GetTimestamp ( ) - st ; if ( mre . IsSet ) return ; } st = Stopwatch . GetTimestamp ( ) ; faster ( ) ; tf += Stopwatch . GetTimestamp ( ) - st ; st = Stopwatch . GetTimestamp ( ) ; slower ( ) ; ts += Stopwatch . GetTimestamp ( ) - st ; if ( mre . IsSet ) return ; r . Add ( tf , ts ) ; if ( r . SigmaSquared >= sigma ) mre . Set ( ) ; } } catch ( Exception e ) { exception = e ; mre . Set ( ) ; } } , null ) ; bool completed = mre . Wait ( timeout * 1000 ) ; if ( raiseexception ) { if ( ! completed ) throw new CsCheckException ( " " + r . ToString ( ) ) ; if ( exception is not null || r . Slower > r . Faster || r . Median . Median < 0.0 ) throw exception ?? new CsCheckException ( r . ToString ( ) ) ; } return r ; }
Assert the first Action is faster than the second to a given sigma.
Assert the first Action is faster than the second to a given sigma .
faster
Action
The presumed faster code to test.
The presumed faster code to test .
slower
Action
The presumed slower code to test.
The presumed slower code to test .
sigma
double
The sigma is the number of standard deviations from the null hypothosis (default 6).
The sigma is the number of standard deviations from the null hypothosis ( default 6 ) .
threads
int
The number of threads to run the code on (default number logical CPUs).
The number of threads to run the code on ( default number logical CPUs ) .
repeat
int
The number of times to call each of the actions in each iteration if they are too quick to accurately measure (default 1).
The number of times to call each of the actions in each iteration if they are too quick to accurately measure ( default 1 ) .
timeout
int
The number of seconds to wait before timing out (default 60).
The number of seconds to wait before timing out ( default 60 ) .
raiseexception
bool
If set an exception will be raised with statistics if slower is actually the fastest (default true).
If set an exception will be raised with statistics if slower is actually the fastest ( default true ) .