Skip to content

Instantly share code, notes, and snippets.

@arman-hpp
Last active April 23, 2017 08:50
Show Gist options
  • Save arman-hpp/3460fe47e4b2290fe1833dcd8a9b02bd to your computer and use it in GitHub Desktop.
Save arman-hpp/3460fe47e4b2290fe1833dcd8a9b02bd to your computer and use it in GitHub Desktop.
bytes
using System;
using System.Diagnostics;
using System.IO;
namespace TestBytes
{
class Program
{
static unsafe void Main(string[] args)
{
var bd1 = File.ReadAllBytes(@"I:\Tools\scala-SDK-4.1.0-vfinal-2.11-win32.win32.x86_64.zip");
var bd2 = File.ReadAllBytes(@"I:\Tools\scala-SDK-4.1.0-vfinal-2.11-win32.win32.x86_64.zip");
var bd3 = File.ReadAllBytes(@"I:\Tools\scala-SDK-4.1.0-vfinal-2.11-win32.win32.x86_64.zip");
var stopwatch = new Stopwatch();
stopwatch.Start();
CallByValue(bd1);
stopwatch.Stop();
Console.WriteLine(stopwatch.ElapsedMilliseconds);
var stopwatch1 = new Stopwatch();
stopwatch1.Start();
fixed (byte* packet = bd2)
{
CallByPointer(packet, bd2.Length);
}
stopwatch1.Stop();
Console.WriteLine(stopwatch1.ElapsedMilliseconds);
var stopwatch2 = new Stopwatch();
stopwatch2.Start();
CallByReference(ref bd3);
stopwatch2.Stop();
Console.WriteLine(stopwatch2.ElapsedMilliseconds);
Console.ReadKey();
}
public static void CallByValue(byte[] bytes)
{
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = bytes[i / 2 + 1];
var t1 = bytes[i] * 2 - 100;
var t2 = t1 + bytes[i] * 2 - 100;
var t3 = bytes[i] * 2 - 100 - t2 / 2;
}
}
public static void CallByReference(ref byte[] bytes)
{
for (var i = 0; i < bytes.Length; i++)
{
bytes[i] = bytes[i / 2 + 1];
var t1 = bytes[i] * 2 - 100;
var t2 = t1 + bytes[i] * 2 - 100;
var t3 = bytes[i] * 2 - 100 - t2/2;
}
}
public static unsafe void CallByPointer(byte* bytes, int len)
{
//byte[] b = new byte[len];
//Marshal.Copy((IntPtr)bytes, b, 0, len);
for (var i = 0; i < len; i++)
{
bytes[i] = bytes[i / 2 + 1];
var t1 = bytes[i] * 2 - 100;
var t2 = t1 + bytes[i] * 2 - 100;
var t3 = bytes[i] * 2 - 100 - t2 / 2;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment