Skip to content

Instantly share code, notes, and snippets.

@yar-shukan
Last active August 29, 2015 13:57
Show Gist options
  • Save yar-shukan/9770086 to your computer and use it in GitHub Desktop.
Save yar-shukan/9770086 to your computer and use it in GitHub Desktop.
TaskRun vs Sync
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Web.Http;
namespace MvcApplication1.Controllers
{
public class WaitController : ApiController
{
[HttpGet]
public async Task<long> TaskRunWait()
{
return await Task.Run(() => WaitOperation());
}
[HttpGet]
public long Wait()
{
return WaitOperation();
}
private static long WaitOperation()
{
int waitTime = WaitTimeGenerator.GetWaitTime();
Stopwatch stopwatch = Stopwatch.StartNew();
for (int i = 0; i < waitTime; i++)
{
}
return stopwatch.ElapsedMilliseconds;
}
}
public static class WaitTimeGenerator
{
private const int Seed = 100;
private static readonly Random Random = new Random(Seed);
public static int GetWaitTime()
{
return Random.Next(900000000, 1000000000);
}
}
}
@yar-shukan
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment