static long MeasureHMHVsPipeline() { const int iterations = 1000; long ran = 0; Adelegate lastFunc = c => Task.FromResult(c.Value + 1); Adelegate a1 = async(context) => { int result = await lastFunc(context); return(result + 1); }; Adelegate a2 = async context => { int result = await a1(context); return(result - 1); }; Bdelegate lastFuncB = c => { c.Value = c.Value + 1; return(TaskConstants.Completed); }; Bdelegate b1 = context => { context.Value = context.Value + 1; return(null); }; Bdelegate b2 = context => { context.Value = context.Value - 1; return(null); }; CodeTimer.TimeAsync( true, "Chained async", iterations, async() => { int result = await a2(new MeasureHPContext()); unchecked { ran += result; } }); Bdelegate[] pipeline = { lastFuncB, b1, b2 }; CodeTimer.TimeAsync( true, "Iterated async", iterations, async() => { var ctx = new MeasureHPContext(); foreach (Bdelegate d in pipeline) { Task t = d(ctx); if (t != null) { await t; } } unchecked { ran += ctx.Value; } }); return(ran); }
static long MeasureHMHVsPipeline() { const int iterations = 1000; long ran = 0; Adelegate lastFunc = c => Task.FromResult(c.Value + 1); Adelegate a1 = async (context) => { int result = await lastFunc(context); return result + 1; }; Adelegate a2 = async context => { int result = await a1(context); return result - 1; }; Bdelegate lastFuncB = c => { c.Value = c.Value + 1; return TaskConstants.Completed; }; Bdelegate b1 = context => { context.Value = context.Value + 1; return null; }; Bdelegate b2 = context => { context.Value = context.Value - 1; return null; }; CodeTimer.TimeAsync( true, "Chained async", iterations, async () => { int result = await a2(new MeasureHPContext()); unchecked { ran += result; } }); Bdelegate[] pipeline = {lastFuncB, b1, b2}; CodeTimer.TimeAsync( true, "Iterated async", iterations, async () => { var ctx = new MeasureHPContext(); foreach (var d in pipeline) { var t = d(ctx); if (t != null) await t; } unchecked { ran += ctx.Value; } }); return ran; }