Skip to content

Instantly share code, notes, and snippets.

@jkotas
Created November 19, 2016 04:34
Show Gist options
  • Save jkotas/319f360c72ba12c3dccc00f557567997 to your computer and use it in GitHub Desktop.
Save jkotas/319f360c72ba12c3dccc00f557567997 to your computer and use it in GitHub Desktop.
using System;
using System.Threading;
using System.Buffers;
class Program
{
#if false
static byte[] AcquireBuffer()
{
return new byte[4096];
}
static void ReleaseBuffer(byte[] b)
{
}
#endif
#if false
[ThreadStatic]
static byte[] manualPool;
static byte[] AcquireBuffer()
{
byte[] ret = manualPool;
manualPool = null;
if (ret == null)
ret = new byte[4096];
return ret;
}
static void ReleaseBuffer(byte[] b)
{
manualPool = b;
}
#endif
#if false
static byte[] AcquireBuffer()
{
return ArrayPool<byte>.Shared.Rent(4096);
}
static void ReleaseBuffer(byte[] b)
{
ArrayPool<byte>.Shared.Return(b);
}
#endif
static int Workitem()
{
byte[] b = AcquireBuffer();
for (int i = 0; i < 4096; i++)
b[i] = (byte)i;
int sum = 0;
for (int i = 0; i < 4096; i++)
sum += b[i];
ReleaseBuffer(b);
return sum;
}
static void Work()
{
for (int i = 0; i < 1000000; i++)
{
Workitem();
}
}
static void Main(string[] args)
{
int start = Environment.TickCount;
Thread[] threads = new Thread[Environment.ProcessorCount];
for (int i = 0; i < threads.Length; i++)
(threads[i] = new Thread(Work)).Start();
for (int i = 0; i < threads.Length; i++)
threads[i].Join();
int end = Environment.TickCount;
Console.WriteLine("Time: " + (end - start));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment