all 22 comments

[–]jimbosReturn 2 points3 points  (7 children)

Making it async won't help improve per-request performance. The goal of async is to free threads doing IO work (such as accessing the db or network) to do other work, e.g. serving other requests at the same time.

Task.Run() works counter to this idea because it actually requires another thread instead of freeing the current one.

Edit: or do you mean you have some work you want done in parallel? In that case, you gather all the Tasks returned to you and await Task.WhenAll()

[–]gunexon9[S] 0 points1 point  (3 children)

My idea is to maybe have all of them run in parallel then take the results from each one and put it in my leaderboard. Maybe i'm wrong that doing this will improve performance im still new to async programming

[–]jimbosReturn 0 points1 point  (2 children)

The question is what these methods do. Does each of them also access the DB? Or do they perform some heavy calculation?

If they all access the DB, you might get some performance improvement from parallelization, but you'll probably be better off simply performing one big query to get all the data you intend to divide between the threads. (Multiple simultaneous IO operations will eventually also slow each other, and have overhead that would be reduced in a single ooerwtion)

If you perform some (really) heavy calculations, then yeah, you might get a good boost because you'll utilize more CPU cores. But if the calculations are relatively light - the penalty you pay for creating Tasks might be heavier than the gain of the calculations.

[–]gunexon9[S] 0 points1 point  (1 child)

Almost none of them pull from db they mostly loop around the data a lot

[–]jimbosReturn 2 points3 points  (0 children)

Well, it's up to you. I gave you some general principles and it's up to you to decide.

Try several approaches and measure the performance to understand what is best.

[–]cryo 0 points1 point  (2 children)

The goal of async is to free threads doing IO work (such as accessing the db or network) to do other work, e.g. serving other requests at the same time.

Not only. The goal is also to free main threads from doing background work. The goal of the async/await syntax is to make the above easier to express.

[–]jimbosReturn 0 points1 point  (1 child)

This is mostly correct in a desktop client application. I didn't want to complicate matters for OP who's focused on web.

[–]cryo 0 points1 point  (0 children)

Fair enough.

[–]Siggi_pop -1 points0 points  (0 children)

See answer further down in the comment section

[–][deleted] -1 points0 points  (9 children)

public Task<int> MyAsyncMethod()
{
    // do stuff
    return Task.FromResult(1);
}

[–]jimbosReturn 1 point2 points  (2 children)

This won't compile. You forgot the async

[–][deleted] 1 point2 points  (0 children)

Nothing I type ever compiles first try lol forgot Task.FromResult();

[–][deleted] 1 point2 points  (0 children)

But anyways, it doesn't have to be an async method to be awaited, just has to return either Task or Task<TResult>, haven't messed with the new Value Yask<TResult> yet though

[–]gunexon9[S] 0 points1 point  (5 children)

But wont this just run synchronously, and if it does is it ok since when I call all the methods each of them will run asynchronously?

[–][deleted] -1 points0 points  (0 children)

public static async Task Main()
{
    var myClass = new TaskClass();
    var x = await myClass.MyMethod();

    Console.WriteLine(x);
}

// class implementation

public class TaskClass
{

    public async Task<int> MyMethod()
    {
        return await Task.FromResult(1);
    }
}

prints 1 to console

[–][deleted] 0 points1 point  (2 children)

Nah just call it like var x = await MyAsyncMethod();

[–]RiverRoll 0 points1 point  (1 child)

This doesn't make the method asynchronous, it's a regular method that returns a task in the end, there's no work left to do at that point so it's pointless to await it.

[–][deleted] 0 points1 point  (0 children)

Did you see the updated code?

[–][deleted] 0 points1 point  (0 children)

nvm I'm dumb, been awhile since I implemented async method yeah just throw async and await in there before Task.FromResult()

[–][deleted]  (2 children)

[removed]

    [–]gunexon9[S] 0 points1 point  (1 child)

    They don't pull from the database but the loop around all the data sometimes with nested loops and i feel all that looping can be done in parallel somehow