c# - What is the overhead of an "synchronized" async method? -
here benchmark methods return task, run synchronizely under hood.
class mainclass { public static async task<int> usingasyncmodifier() { return 10; } public static task<int> usingtaskcompletionsource() { taskcompletionsource<int> tcs = new taskcompletionsource<int>(); tcs.setresult(10); return tcs.task; } public static task<int> usingtaskfromresult() { return task.fromresult(10); } public static void main(string[] args) { datetime t = datetime.now; const int repeat = 10000; // results volatile while repeat grows. console.writeline("repeat {0} times.", repeat); int j = 0; (int = 0; < repeat; i++) { j += usingasyncmodifier().result; } console.writeline("usingasyncmodifier: {0}", datetime.now - t); t = datetime.now; (int = 0; < repeat; i++) { j += usingtaskcompletionsource().result; } console.writeline("usingtaskcompletionsource: {0}", datetime.now - t); t = datetime.now; (int = 0; < repeat; i++) { j += usingtaskfromresult().result; } console.writeline("usingtaskfromresult: {0}", datetime.now - t); } } output (repeat 10,000/100,000/1000,000 times):
repeat 10000 times. usingasyncmodifier: 00:00:00.1043980 usingtaskcompletionsource: 00:00:00.0095270 usingtaskfromresult: 00:00:00.0089460 repeat 10,000 times, usingtaskfromresult 10x faster usingasyncmodifier.
repeat 100000 times. usingasyncmodifier: 00:00:00.1676000 usingtaskcompletionsource: 00:00:00.0872020 usingtaskfromresult: 00:00:00.0870180 repeat 100,000 times, usingtaskfromresult 2x faster usingasyncmodifier.
repeat 1000000 times. usingasyncmodifier: 00:00:00.8458490 usingtaskcompletionsource: 00:00:00.8870980 usingtaskfromresult: 00:00:00.9027320 repeat 1,000,000 times, usingasyncmodifier faster usingtaskfromresult.
what think was, async modifier created completed task, task.fromresult() does. benchmark not prove idea. why?
while see similar results using datetime, use of stopwatch time measuring shows iterations using usingasyncmodifier() take 2 times more time duration (than using usingtaskcompletionsource() or usingtaskfromresult(), both showing equal appr. duration) 1 000 000 iterations
here output:
repeat 1000000 times. usingasyncmodifier: 5458 usingtaskcompletionsource: 2838 usingtaskfromresult: 2556 with code using stopwatch
class program { public static async task<int> usingasyncmodifier() { return 10; } public static task<int> usingtaskcompletionsource() { taskcompletionsource<int> tcs = new taskcompletionsource<int>(); tcs.setresult(10); return tcs.task; } public static task<int> usingtaskfromresult() { return taskex.fromresult(10); } static void main(string[] args) { //datetime t = datetime.now; stopwatch timer = new stopwatch(); const int repeat = 1000*1000; // results volatile while repeat grows. console.writeline("repeat {0} times.", repeat); int j = 0; //datetime t = datetime.now; timer.start(); (int = 0; < repeat; i++) { j += usingasyncmodifier().result; } timer.stop(); console.writeline("usingasyncmodifier: {0}" , timer.elapsedmilliseconds); //t = datetime.now; timer.reset(); j = 0; timer.start(); (int = 0; < repeat; i++) { j += usingtaskcompletionsource().result; } timer.stop(); console.writeline("usingtaskcompletionsource: {0}" , timer.elapsedmilliseconds); //t = datetime.now; timer.reset(); j = 0; timer.start(); (int = 0; < repeat; i++) { j += usingtaskfromresult().result; } timer.stop(); console.writeline("usingtaskfromresult: {0}" , timer.elapsedmilliseconds); console.readline(); } } stephen toub in "async performance: understanding costs of async , await" explains:
when working synchronous code, methods empty bodies practically free. not case asynchronous methods
read more details
Comments
Post a Comment