Files
TouchSocket/examples/BytePool简单示例/BytePoolConsoleApp/Program.cs
2023-02-07 19:53:23 +08:00

66 lines
2.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using TouchSocket.Core;
namespace BytePoolConsoleApp
{
internal class Program
{
private static void Main(string[] args)
{
Console.ReadKey();
BytePool.Default.AddSizeKey(1024 * 1024);
//BytePool.AutoZero = true;
for (int i = 0; i < 5; i++)
{
byte[] data = BytePool.Default.GetByteCore(1024 * 10, true);
BytePool.Default.Recycle(data);
using (ByteBlock byteBlock = new ByteBlock(1024 * 10, true))
{
//最重要千万不要引用byteBlock.Buffer
byteBlock.Write(10);
byteBlock.Write('A');
byteBlock.Write(100L);
byteBlock.Write(3.1415926);
byteBlock.Write("Hello TouchSocket");
var buffer = byteBlock.ToArray();
byteBlock.Position = 0;
var p1 = byteBlock.ReadInt32();
var p2 = byteBlock.ReadChar();
var p3 = byteBlock.ReadInt64();
var p4 = byteBlock.ReadDouble();
var p5 = byteBlock.ReadString();
}
}
Console.ReadKey();
}
private static void Performance()
{
int count = 1000000;
TimeSpan timeSpan1 = TimeMeasurer.Run(() =>
{
for (int i = 0; i < count; i++)
{
byte[] buffer = new byte[1024];
}
});
TimeSpan timeSpan2 = TimeMeasurer.Run(() =>
{
for (int i = 0; i < count; i++)
{
ByteBlock byteBlock = new ByteBlock(1024, true);
byteBlock.Dispose();
}
});
Console.WriteLine($"直接实例化:{timeSpan1}");
Console.WriteLine($"内存池实例化:{timeSpan2}");
}
}
}