Thursday, 15 July 2010

c# - Async Await performance - Direct method call vs Task wrapper call -



c# - Async Await performance - Direct method call vs Task wrapper call -

a little programme have created understand working of async await , calling async method in loop, direct method call:

sumprogram.callsum(i, + 1);

or using task apis task.run / task.factory.startnew

my initial understanding task api create substantially faster, contrary expectation, reflect lack in understanding, direct phone call much improve in terms of performance. in fact when additional thread sleep introduced in getsum method, seems impacting task calls , big margin.

now understand first part of direct phone call beingness faster, since made execute asynchronously , there's no overhead of adding list of tasks , making them wait, when shift same technique real programming paradigm, question remains:

for direct call, there's nil emulate task.waitall, exit calling method, if executions not complete, alternative task wrapper.

am getting convoluted results, since direct phone call stopwatch publish time before executions finish , not case task wrapper due waitall

for executing program, need comment / uncomment relevant portions right results

class programme { static void main(string[] args) { programme sumprogram = new program(); list<task> tasklist = new list<task>(); // task stopwatch sw = stopwatch.startnew(); (int = 0; < 100000; i++) { tasklist.add(task.factory.startnew(() => { sumprogram.callsum(i, + 1); })); // task utilize 1 tasklist.add(task.run(() => { sumprogram.callsum(i, + 1); })); // task utilize 1 sumprogram.callsum(i, + 1); } task.waitall(tasklist.toarray()); // task sw.stop(); console.writeline("time elapsed :: " + sw.elapsedmilliseconds); } public async task callsum(int num1, int num2) { func<int> callfunc = (() => { homecoming getsum(num1, num2); }); int result = await task.run<int>(callfunc); //console.writeline("sum result :: " + result); } public int getsum(int num1, int num2) { thread.sleep(10); homecoming (num1 + num2); } }

the big problem code node of options you're using waiting callsum() complete. that, take task that's returned callsum() , wait it. example:

tasklist.add(sumprogram.callsum(i, + 1));

c# .net asynchronous task-parallel-library task

No comments:

Post a Comment