Run fast things synchronously and things delayed asynchronously?

2

Generally, in .NET, I run time-consuming things asynchronously so I do not catch the visual thread . In a simple example, if I have a loading in the UI, in case slow things run without awaited , they catch that loading , which was executed in thread visual.

Actions that may be time-consuming, such as processing a list, loops or reading files, I execute asynchronously. But when I have things I know are not time-consuming, like a simple conditional structure:

public bool? ToBoolean(int input)
{
    if (input == 1)
    {
        return true;
    }
    else if (input == 0)
    {
        return false;
    }

    return null;
}

For these cases, which I know are fast (in theory), should I have something of the type?

public async Task<bool?> ToBooleanAsync(int input)
{
  return await Task.Run(() => ToBoolean(input));
}

Should I execute fast things asynchronously, and why (or why not)?

    
asked by anonymous 18.02.2017 / 11:41

1 answer

4

No, you should not do this at all. In this specific case, setting up and controlling the entire infrastructure of asynchronicity will take more time than the execution of this method.

The recommendation for the asynchronous call offset is the method take 50 milliseconds (on the user's machine, not on his powerful developer). Even because a time less than this is not very much perceived by the human, and doing something asynchronous only serves to improve the user experience. It does not perform better or faster. On the contrary, it is even slower.

If it runs on a server, in batch , out of user interaction, asynchronicity does not help in the same way. It depends whether a developed solution has a single execution queue or there may be independent executions, maybe until asynchronicity is used to allow more than one line of execution, not a clear rule, it has to measure. Certainly something so simple does not make up for that. Many times it will only make up for much higher times.

    
18.02.2017 / 12:44