Advertisement
Guest User

Untitled

a guest
Oct 5th, 2022
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 54.89 KB | None | 0 0
  1. using System.Diagnostics;
  2. using System.Runtime.CompilerServices;
  3. using System.Runtime.InteropServices;
  4. using System.Runtime.Intrinsics;
  5. using System.Runtime.Intrinsics.X86;
  6.  
  7. namespace ClearBufferTest {
  8.     internal unsafe class Program {
  9.         static byte[]? ByteFrame;
  10.         static byte[]? ByteFrameClearer;
  11.         static Int32[]? Int32Frame;
  12.         static Int32[]? Int32FrameClearer;
  13.         static float[]? FloatFrame;
  14.         static float[]? FloatFrameClearer;
  15.         static int[]? ResetCacheArray;
  16.         static string ParseDouble(double d) {
  17.             string doubleString = d.ToString();
  18.             doubleString = doubleString.Replace(".", ",");
  19.             if (doubleString.IndexOf(',') > -1 && doubleString.Length > doubleString.IndexOf(',')+3 ) {
  20.                 doubleString = doubleString.Substring(0, doubleString.IndexOf(',') + 3);
  21.             }
  22.             return doubleString;
  23.         }
  24.         static void Main(string[] args) {
  25.             System.Diagnostics.Process.GetCurrentProcess().PriorityClass = System.Diagnostics.ProcessPriorityClass.RealTime;
  26.             // size vars
  27.             int Width = 1500;
  28.             int Height = 1500;
  29.             Console.WriteLine(Width+"*"+Height);
  30.             //
  31.             byte byteValue = (byte)5;
  32.             int intValue = 5;
  33.             int int64Value = 5;
  34.             float floatValue = 5f;
  35.             // Init frames
  36.             ByteFrame = new byte[Width * Height * 4];
  37.             ByteFrame.AsSpan().Fill(byteValue);
  38.             ByteFrameClearer = new byte[Width * Height * 4];
  39.             ByteFrameClearer.AsSpan().Fill(byteValue);
  40.             Int32Frame = new Int32[Width * Height];
  41.             Int32Frame.AsSpan().Fill(intValue);
  42.             Int32FrameClearer = new Int32[Width * Height];
  43.             Int32FrameClearer.AsSpan().Fill(intValue);
  44.             FloatFrame = new float[Width * Height];
  45.             FloatFrame.AsSpan().Fill(floatValue);
  46.             FloatFrameClearer = new float[Width * Height];
  47.             FloatFrameClearer.AsSpan().Fill(floatValue);
  48.             ResetCacheArray = new int[20000 * 10000];
  49.             ResetCacheArray.AsSpan().Fill(intValue);
  50.  
  51.  
  52.             // warmup jitter
  53.             for (int i = 0; i < 150; i++) {
  54.                 ClearByteFrameAsSpanFill(byteValue);
  55.                 ClearByteFrameArrayFill(byteValue);
  56.                 ClearByteFrameAsByteAVX(byteValue);
  57.                 ClearByteFrameAsByteAVXUnRolled(byteValue);
  58.                 ClearByteFrameAsByteAVXThreaded(byteValue, 12);
  59.                 ClearByteFrameMarshalCopy(byteValue);
  60.                 ClearByteFrameMarshalCopy4(byteValue);
  61.                 ClearByteFrameMarshalThreaded(byteValue, 12);
  62.                 ClearByteFrameNaive(byteValue);
  63.                 ClearByteFrameBytePointer(byteValue);
  64.                 ClearByteFrameInt32Pointer(intValue);
  65.                 ClearByteFrameInt32PointerThreads(intValue, 12);
  66.                 ClearByteFrameInt64Pointer(int64Value);
  67.                 ClearByteFrameInt64PointerThreads(int64Value, 12);
  68.  
  69.                 ClearInt32FrameAsSpanFill(intValue);
  70.                 ClearInt32FrameArrayFill(intValue);
  71.  
  72.                 ClearFloatFrameAsSpanFill(floatValue);
  73.                 ClearFloatFrameArrayFill(floatValue);
  74.  
  75.                 ClearCache();
  76.             }
  77.  
  78.  
  79.             Console.WriteLine(Environment.Is64BitProcess);
  80.  
  81.             int TestIterations;
  82.             double nanoseconds;
  83.             double MsDuration;
  84.             double MB = 0;
  85.             double MBSec;
  86.             double GBSec;
  87.             List<double> MSAr = new List<double>();
  88.             List<double> MBSAr = new List<double>();
  89.             TestIterations = 20;
  90.  
  91.  
  92.             for (int i = 0; i < TestIterations; i++) {
  93.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  94.                 MB = ClearByteFrameAsSpanFill(byteValue);
  95.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  96.                 MSAr.Add(MsDuration);
  97.                 MBSec = (MB / MsDuration) * 1000;
  98.                 MBSAr.Add(MBSec);
  99.                 ClearCache();
  100.             }
  101.             MsDuration = MSAr.Sum() / TestIterations;
  102.             MBSec = MBSAr.Sum() / TestIterations;
  103.             GBSec = MBSec / 1000;
  104.             Console.WriteLine("ClearByteFrameAsSpanFill:              MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  105.             MSAr.Clear();
  106.             MBSAr.Clear();
  107.  
  108.             for (int i = 0; i < TestIterations; i++) {
  109.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  110.                 MB = ClearByteFrameArrayFill(byteValue);
  111.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  112.                 MSAr.Add(MsDuration);
  113.                 MBSec = (MB / MsDuration) * 1000;
  114.                 MBSAr.Add(MBSec);
  115.                 ClearCache();
  116.             }
  117.             MsDuration = MSAr.Sum() / TestIterations;
  118.             MBSec = MBSAr.Sum() / TestIterations;
  119.             GBSec = MBSec / 1000;
  120.             Console.WriteLine("ClearByteFrameArrayFill:               MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  121.             MSAr.Clear();
  122.             MBSAr.Clear();
  123.  
  124.             for (int i = 0; i < TestIterations; i++) {
  125.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  126.                 MB = ClearByteFrameAsByteAVX(byteValue);
  127.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  128.                 MSAr.Add(MsDuration);
  129.                 MBSec = (MB / MsDuration) * 1000;
  130.                 MBSAr.Add(MBSec);
  131.                 ClearCache();
  132.             }
  133.             MsDuration = MSAr.Sum() / TestIterations;
  134.             MBSec = MBSAr.Sum() / TestIterations;
  135.             GBSec = MBSec / 1000;
  136.             Console.WriteLine("ClearByteFrameAsByteAVX:               MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  137.             MSAr.Clear();
  138.             MBSAr.Clear();
  139.  
  140.             for (int i = 0; i < TestIterations; i++) {
  141.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  142.                 MB = ClearByteFrameAsByteAVXUnRolled(byteValue);
  143.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  144.                 MSAr.Add(MsDuration);
  145.                 MBSec = (MB / MsDuration) * 1000;
  146.                 MBSAr.Add(MBSec);
  147.                 ClearCache();
  148.             }
  149.             MsDuration = MSAr.Sum() / TestIterations;
  150.             MBSec = MBSAr.Sum() / TestIterations;
  151.             GBSec = MBSec / 1000;
  152.             Console.WriteLine("ClearByteFrameAsByteAVXUnRolled:       MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  153.             MSAr.Clear();
  154.             MBSAr.Clear();
  155.  
  156.             for (int i = 0; i < TestIterations; i++) {
  157.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  158.                 MB = ClearByteFrameAsByteAVXThreaded(byteValue, 2);
  159.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  160.                 MSAr.Add(MsDuration);
  161.                 MBSec = (MB / MsDuration) * 1000;
  162.                 MBSAr.Add(MBSec);
  163.                 ClearCache();
  164.             }
  165.             MsDuration = MSAr.Sum() / TestIterations;
  166.             MBSec = MBSAr.Sum() / TestIterations;
  167.             GBSec = MBSec / 1000;
  168.             Console.WriteLine("ClearByteFrameAsByteAVXThreaded(2):    MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  169.             MSAr.Clear();
  170.             MBSAr.Clear();
  171.  
  172.             for (int i = 0; i < TestIterations; i++) {
  173.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  174.                 MB = ClearByteFrameAsByteAVXThreaded(byteValue, 4);
  175.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  176.                 MSAr.Add(MsDuration);
  177.                 MBSec = (MB / MsDuration) * 1000;
  178.                 MBSAr.Add(MBSec);
  179.                 ClearCache();
  180.             }
  181.             MsDuration = MSAr.Sum() / TestIterations;
  182.             MBSec = MBSAr.Sum() / TestIterations;
  183.             GBSec = MBSec / 1000;
  184.             Console.WriteLine("ClearByteFrameAsByteAVXThreaded(4):    MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  185.             MSAr.Clear();
  186.             MBSAr.Clear();
  187.  
  188.             for (int i = 0; i < TestIterations; i++) {
  189.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  190.                 MB = ClearByteFrameAsByteAVXThreaded(byteValue, 6);
  191.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  192.                 MSAr.Add(MsDuration);
  193.                 MBSec = (MB / MsDuration) * 1000;
  194.                 MBSAr.Add(MBSec);
  195.                 ClearCache();
  196.             }
  197.             MsDuration = MSAr.Sum() / TestIterations;
  198.             MBSec = MBSAr.Sum() / TestIterations;
  199.             GBSec = MBSec / 1000;
  200.             Console.WriteLine("ClearByteFrameAsByteAVXThreaded(6):    MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  201.             MSAr.Clear();
  202.             MBSAr.Clear();
  203.  
  204.             for (int i = 0; i < TestIterations; i++) {
  205.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  206.                 MB = ClearByteFrameAsByteAVXThreaded(byteValue, 8);
  207.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  208.                 MSAr.Add(MsDuration);
  209.                 MBSec = (MB / MsDuration) * 1000;
  210.                 MBSAr.Add(MBSec);
  211.                 ClearCache();
  212.             }
  213.             MsDuration = MSAr.Sum() / TestIterations;
  214.             MBSec = MBSAr.Sum() / TestIterations;
  215.             GBSec = MBSec / 1000;
  216.             Console.WriteLine("ClearByteFrameAsByteAVXThreaded(8):    MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  217.             MSAr.Clear();
  218.             MBSAr.Clear();
  219.  
  220.             for (int i = 0; i < TestIterations; i++) {
  221.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  222.                 MB = ClearByteFrameAsByteAVXThreaded(byteValue, 12);
  223.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  224.                 MSAr.Add(MsDuration);
  225.                 MBSec = (MB / MsDuration) * 1000;
  226.                 MBSAr.Add(MBSec);
  227.                 ClearCache();
  228.             }
  229.             MsDuration = MSAr.Sum() / TestIterations;
  230.             MBSec = MBSAr.Sum() / TestIterations;
  231.             GBSec = MBSec / 1000;
  232.             Console.WriteLine("ClearByteFrameAsByteAVXThreaded(12):   MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  233.             MSAr.Clear();
  234.             MBSAr.Clear();
  235.  
  236.             for (int i = 0; i < TestIterations; i++) {
  237.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  238.                 MB = ClearByteFrameMarshalCopy(byteValue);
  239.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  240.                 MSAr.Add(MsDuration);
  241.                 MBSec = (MB / MsDuration) * 1000;
  242.                 MBSAr.Add(MBSec);
  243.                 ClearCache();
  244.             }
  245.             MsDuration = MSAr.Sum() / TestIterations;
  246.             MBSec = MBSAr.Sum() / TestIterations;
  247.             GBSec = MBSec / 1000;
  248.             Console.WriteLine("ClearByteFrameMarshalCopy:             MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  249.             MSAr.Clear();
  250.             MBSAr.Clear();
  251.  
  252.             for (int i = 0; i < TestIterations; i++) {
  253.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  254.                 MB = ClearByteFrameMarshalCopy4(byteValue);
  255.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  256.                 MSAr.Add(MsDuration);
  257.                 MBSec = (MB / MsDuration) * 1000;
  258.                 MBSAr.Add(MBSec);
  259.                 ClearCache();
  260.             }
  261.             MsDuration = MSAr.Sum() / TestIterations;
  262.             MBSec = MBSAr.Sum() / TestIterations;
  263.             GBSec = MBSec / 1000;
  264.             Console.WriteLine("ClearByteFrameMarshalCopy4:            MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  265.             MSAr.Clear();
  266.             MBSAr.Clear();
  267.  
  268.             for (int i = 0; i < TestIterations; i++) {
  269.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  270.                 MB = ClearByteFrameMarshalThreaded(byteValue, 2);
  271.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  272.                 MSAr.Add(MsDuration);
  273.                 MBSec = (MB / MsDuration) * 1000;
  274.                 MBSAr.Add(MBSec);
  275.                 ClearCache();
  276.             }
  277.             MsDuration = MSAr.Sum() / TestIterations;
  278.             MBSec = MBSAr.Sum() / TestIterations;
  279.             GBSec = MBSec / 1000;
  280.             Console.WriteLine("ClearByteFrameMarshalThreaded(2):      MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  281.             MSAr.Clear();
  282.             MBSAr.Clear();
  283.  
  284.             for (int i = 0; i < TestIterations; i++) {
  285.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  286.                 MB = ClearByteFrameMarshalThreaded(byteValue, 4);
  287.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  288.                 MSAr.Add(MsDuration);
  289.                 MBSec = (MB / MsDuration) * 1000;
  290.                 MBSAr.Add(MBSec);
  291.                 ClearCache();
  292.             }
  293.             MsDuration = MSAr.Sum() / TestIterations;
  294.             MBSec = MBSAr.Sum() / TestIterations;
  295.             GBSec = MBSec / 1000;
  296.             Console.WriteLine("ClearByteFrameMarshalThreaded(4):      MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  297.             MSAr.Clear();
  298.             MBSAr.Clear();
  299.  
  300.             for (int i = 0; i < TestIterations; i++) {
  301.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  302.                 MB = ClearByteFrameMarshalThreaded(byteValue, 6);
  303.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  304.                 MSAr.Add(MsDuration);
  305.                 MBSec = (MB / MsDuration) * 1000;
  306.                 MBSAr.Add(MBSec);
  307.                 ClearCache();
  308.             }
  309.             MsDuration = MSAr.Sum() / TestIterations;
  310.             MBSec = MBSAr.Sum() / TestIterations;
  311.             GBSec = MBSec / 1000;
  312.             Console.WriteLine("ClearByteFrameMarshalThreaded(6):      MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  313.             MSAr.Clear();
  314.             MBSAr.Clear();
  315.  
  316.             for (int i = 0; i < TestIterations; i++) {
  317.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  318.                 MB = ClearByteFrameMarshalThreaded(byteValue, 8);
  319.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  320.                 MSAr.Add(MsDuration);
  321.                 MBSec = (MB / MsDuration) * 1000;
  322.                 MBSAr.Add(MBSec);
  323.                 ClearCache();
  324.             }
  325.             MsDuration = MSAr.Sum() / TestIterations;
  326.             MBSec = MBSAr.Sum() / TestIterations;
  327.             GBSec = MBSec / 1000;
  328.             Console.WriteLine("ClearByteFrameMarshalThreaded(8):      MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  329.             MSAr.Clear();
  330.             MBSAr.Clear();
  331.  
  332.             for (int i = 0; i < TestIterations; i++) {
  333.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  334.                 MB = ClearByteFrameMarshalThreaded(byteValue, 12);
  335.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  336.                 MSAr.Add(MsDuration);
  337.                 MBSec = (MB / MsDuration) * 1000;
  338.                 MBSAr.Add(MBSec);
  339.                 ClearCache();
  340.             }
  341.             MsDuration = MSAr.Sum() / TestIterations;
  342.             MBSec = MBSAr.Sum() / TestIterations;
  343.             GBSec = MBSec / 1000;
  344.             Console.WriteLine("ClearByteFrameMarshalThreaded(12):     MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  345.             MSAr.Clear();
  346.             MBSAr.Clear();
  347.  
  348.             for (int i = 0; i < TestIterations; i++) {
  349.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  350.                 MB = ClearByteFrameNaive(byteValue);
  351.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  352.                 MSAr.Add(MsDuration);
  353.                 MBSec = (MB / MsDuration) * 1000;
  354.                 MBSAr.Add(MBSec);
  355.                 ClearCache();
  356.             }
  357.             MsDuration = MSAr.Sum() / TestIterations;
  358.             MBSec = MBSAr.Sum() / TestIterations;
  359.             GBSec = MBSec / 1000;
  360.             Console.WriteLine("ClearByteFrameNaive:                   MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  361.             MSAr.Clear();
  362.             MBSAr.Clear();
  363.  
  364.             for (int i = 0; i < TestIterations; i++) {
  365.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  366.                 MB = ClearByteFrameBytePointer(byteValue);
  367.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  368.                 MSAr.Add(MsDuration);
  369.                 MBSec = (MB / MsDuration) * 1000;
  370.                 MBSAr.Add(MBSec);
  371.                 ClearCache();
  372.             }
  373.             MsDuration = MSAr.Sum() / TestIterations;
  374.             MBSec = MBSAr.Sum() / TestIterations;
  375.             GBSec = MBSec / 1000;
  376.             Console.WriteLine("ClearByteFrameBytePointer:             MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  377.             MSAr.Clear();
  378.             MBSAr.Clear();
  379.  
  380.             for (int i = 0; i < TestIterations; i++) {
  381.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  382.                 MB = ClearByteFrameInt32Pointer(byteValue);
  383.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  384.                 MSAr.Add(MsDuration);
  385.                 MBSec = (MB / MsDuration) * 1000;
  386.                 MBSAr.Add(MBSec);
  387.                 ClearCache();
  388.             }
  389.             MsDuration = MSAr.Sum() / TestIterations;
  390.             MBSec = MBSAr.Sum() / TestIterations;
  391.             GBSec = MBSec / 1000;
  392.             Console.WriteLine("ClearByteFrameInt32Pointer:            MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  393.             MSAr.Clear();
  394.             MBSAr.Clear();
  395.  
  396.             for (int i = 0; i < TestIterations; i++) {
  397.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  398.                 MB = ClearByteFrameInt32PointerThreads(intValue, 2);
  399.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  400.                 MSAr.Add(MsDuration);
  401.                 MBSec = (MB / MsDuration) * 1000;
  402.                 MBSAr.Add(MBSec);
  403.                 ClearCache();
  404.             }
  405.             MsDuration = MSAr.Sum() / TestIterations;
  406.             MBSec = MBSAr.Sum() / TestIterations;
  407.             GBSec = MBSec / 1000;
  408.             Console.WriteLine("ClearByteFrameInt32PointerThreads(2):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  409.             MSAr.Clear();
  410.             MBSAr.Clear();
  411.  
  412.             for (int i = 0; i < TestIterations; i++) {
  413.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  414.                 MB = ClearByteFrameInt32PointerThreads(intValue, 4);
  415.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  416.                 MSAr.Add(MsDuration);
  417.                 MBSec = (MB / MsDuration) * 1000;
  418.                 MBSAr.Add(MBSec);
  419.                 ClearCache();
  420.             }
  421.             MsDuration = MSAr.Sum() / TestIterations;
  422.             MBSec = MBSAr.Sum() / TestIterations;
  423.             GBSec = MBSec / 1000;
  424.             Console.WriteLine("ClearByteFrameInt32PointerThreads(4):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  425.             MSAr.Clear();
  426.             MBSAr.Clear();
  427.  
  428.             for (int i = 0; i < TestIterations; i++) {
  429.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  430.                 MB = ClearByteFrameInt32PointerThreads(intValue, 6);
  431.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  432.                 MSAr.Add(MsDuration);
  433.                 MBSec = (MB / MsDuration) * 1000;
  434.                 MBSAr.Add(MBSec);
  435.                 ClearCache();
  436.             }
  437.             MsDuration = MSAr.Sum() / TestIterations;
  438.             MBSec = MBSAr.Sum() / TestIterations;
  439.             GBSec = MBSec / 1000;
  440.             Console.WriteLine("ClearByteFrameInt32PointerThreads(6):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  441.             MSAr.Clear();
  442.             MBSAr.Clear();
  443.  
  444.             for (int i = 0; i < TestIterations; i++) {
  445.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  446.                 MB = ClearByteFrameInt32PointerThreads(intValue, 8);
  447.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  448.                 MSAr.Add(MsDuration);
  449.                 MBSec = (MB / MsDuration) * 1000;
  450.                 MBSAr.Add(MBSec);
  451.                 ClearCache();
  452.             }
  453.             MsDuration = MSAr.Sum() / TestIterations;
  454.             MBSec = MBSAr.Sum() / TestIterations;
  455.             GBSec = MBSec / 1000;
  456.             Console.WriteLine("ClearByteFrameInt32PointerThreads(8):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  457.             MSAr.Clear();
  458.             MBSAr.Clear();
  459.  
  460.             for (int i = 0; i < TestIterations; i++) {
  461.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  462.                 MB = ClearByteFrameInt32PointerThreads(intValue, 12);
  463.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  464.                 MSAr.Add(MsDuration);
  465.                 MBSec = (MB / MsDuration) * 1000;
  466.                 MBSAr.Add(MBSec);
  467.                 ClearCache();
  468.             }
  469.             MsDuration = MSAr.Sum() / TestIterations;
  470.             MBSec = MBSAr.Sum() / TestIterations;
  471.             GBSec = MBSec / 1000;
  472.             Console.WriteLine("ClearByteFrameInt32PointerThreads(12): MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  473.             MSAr.Clear();
  474.             MBSAr.Clear();
  475.  
  476.             for (int i = 0; i < TestIterations; i++) {
  477.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  478.                 MB = ClearByteFrameInt64Pointer(byteValue);
  479.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  480.                 MSAr.Add(MsDuration);
  481.                 MBSec = (MB / MsDuration) * 1000;
  482.                 MBSAr.Add(MBSec);
  483.                 ClearCache();
  484.             }
  485.             MsDuration = MSAr.Sum() / TestIterations;
  486.             MBSec = MBSAr.Sum() / TestIterations;
  487.             GBSec = MBSec / 1000;
  488.             Console.WriteLine("ClearByteFrameInt64Pointer:            MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  489.             MSAr.Clear();
  490.             MBSAr.Clear();
  491.  
  492.             for (int i = 0; i < TestIterations; i++) {
  493.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  494.                 MB = ClearByteFrameInt64PointerThreads(int64Value, 2);
  495.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  496.                 MSAr.Add(MsDuration);
  497.                 MBSec = (MB / MsDuration) * 1000;
  498.                 MBSAr.Add(MBSec);
  499.                 ClearCache();
  500.             }
  501.             MsDuration = MSAr.Sum() / TestIterations;
  502.             MBSec = MBSAr.Sum() / TestIterations;
  503.             GBSec = MBSec / 1000;
  504.             Console.WriteLine("ClearByteFrameInt64PointerThreads(2):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  505.             MSAr.Clear();
  506.             MBSAr.Clear();
  507.  
  508.             for (int i = 0; i < TestIterations; i++) {
  509.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  510.                 MB = ClearByteFrameInt64PointerThreads(int64Value, 4);
  511.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  512.                 MSAr.Add(MsDuration);
  513.                 MBSec = (MB / MsDuration) * 1000;
  514.                 MBSAr.Add(MBSec);
  515.                 ClearCache();
  516.             }
  517.             MsDuration = MSAr.Sum() / TestIterations;
  518.             MBSec = MBSAr.Sum() / TestIterations;
  519.             GBSec = MBSec / 1000;
  520.             Console.WriteLine("ClearByteFrameInt64PointerThreads(4):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  521.             MSAr.Clear();
  522.             MBSAr.Clear();
  523.  
  524.             for (int i = 0; i < TestIterations; i++) {
  525.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  526.                 MB = ClearByteFrameInt64PointerThreads(int64Value, 6);
  527.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  528.                 MSAr.Add(MsDuration);
  529.                 MBSec = (MB / MsDuration) * 1000;
  530.                 MBSAr.Add(MBSec);
  531.                 ClearCache();
  532.             }
  533.             MsDuration = MSAr.Sum() / TestIterations;
  534.             MBSec = MBSAr.Sum() / TestIterations;
  535.             GBSec = MBSec / 1000;
  536.             Console.WriteLine("ClearByteFrameInt64PointerThreads(6):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  537.             MSAr.Clear();
  538.             MBSAr.Clear();
  539.  
  540.             for (int i = 0; i < TestIterations; i++) {
  541.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  542.                 MB = ClearByteFrameInt64PointerThreads(int64Value, 8);
  543.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  544.                 MSAr.Add(MsDuration);
  545.                 MBSec = (MB / MsDuration) * 1000;
  546.                 MBSAr.Add(MBSec);
  547.                 ClearCache();
  548.             }
  549.             MsDuration = MSAr.Sum() / TestIterations;
  550.             MBSec = MBSAr.Sum() / TestIterations;
  551.             GBSec = MBSec / 1000;
  552.             Console.WriteLine("ClearByteFrameInt64PointerThreads(8):  MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  553.             MSAr.Clear();
  554.             MBSAr.Clear();
  555.  
  556.             for (int i = 0; i < TestIterations; i++) {
  557.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  558.                 MB = ClearByteFrameInt64PointerThreads(int64Value, 12);
  559.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  560.                 MSAr.Add(MsDuration);
  561.                 MBSec = (MB / MsDuration) * 1000;
  562.                 MBSAr.Add(MBSec);
  563.                 ClearCache();
  564.             }
  565.             MsDuration = MSAr.Sum() / TestIterations;
  566.             MBSec = MBSAr.Sum() / TestIterations;
  567.             GBSec = MBSec / 1000;
  568.             Console.WriteLine("ClearByteFrameInt64PointerThreads(12): MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  569.             MSAr.Clear();
  570.             MBSAr.Clear();
  571.  
  572.  
  573.             Console.WriteLine();
  574.  
  575.  
  576.  
  577.  
  578.  
  579.  
  580.             for (int i = 0; i < TestIterations; i++) {
  581.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  582.                 MB = ClearInt32FrameAsSpanFill(intValue);
  583.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  584.                 MSAr.Add(MsDuration);
  585.                 MBSec = (MB / MsDuration) * 1000;
  586.                 MBSAr.Add(MBSec);
  587.                 ClearCache();
  588.             }
  589.             MsDuration = MSAr.Sum() / TestIterations;
  590.             MBSec = MBSAr.Sum() / TestIterations;
  591.             GBSec = MBSec / 1000;
  592.             Console.WriteLine("ClearInt32FrameAsSpanFill:             MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  593.             MSAr.Clear();
  594.             MBSAr.Clear();
  595.  
  596.             for (int i = 0; i < TestIterations; i++) {
  597.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  598.                 MB = ClearInt32FrameArrayFill(intValue);
  599.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  600.                 MSAr.Add(MsDuration);
  601.                 MBSec = (MB / MsDuration) * 1000;
  602.                 MBSAr.Add(MBSec);
  603.                 ClearCache();
  604.             }
  605.             MsDuration = MSAr.Sum() / TestIterations;
  606.             MBSec = MBSAr.Sum() / TestIterations;
  607.             GBSec = MBSec / 1000;
  608.             Console.WriteLine("ClearInt32FrameArrayFill:              MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  609.             MSAr.Clear();
  610.             MBSAr.Clear();
  611.  
  612.  
  613.             Console.WriteLine();
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622.             for (int i = 0; i < TestIterations; i++) {
  623.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  624.                 MB = ClearFloatFrameAsSpanFill(floatValue);
  625.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  626.                 MSAr.Add(MsDuration);
  627.                 MBSec = (MB / MsDuration) * 1000;
  628.                 MBSAr.Add(MBSec);
  629.                 ClearCache();
  630.             }
  631.             MsDuration = MSAr.Sum() / TestIterations;
  632.             MBSec = MBSAr.Sum() / TestIterations;
  633.             GBSec = MBSec / 1000;
  634.             Console.WriteLine("ClearFloatFrameAsSpanFill:             MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  635.             MSAr.Clear();
  636.             MBSAr.Clear();
  637.  
  638.             for (int i = 0; i < TestIterations; i++) {
  639.                 nanoseconds = 1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency;
  640.                 MB = ClearFloatFrameArrayFill(floatValue);
  641.                 MsDuration = (((1_000_000_000.0 * Stopwatch.GetTimestamp() / Stopwatch.Frequency) - nanoseconds)) / 1000000;
  642.                 MSAr.Add(MsDuration);
  643.                 MBSec = (MB / MsDuration) * 1000;
  644.                 MBSAr.Add(MBSec);
  645.                 ClearCache();
  646.             }
  647.             MsDuration = MSAr.Sum() / TestIterations;
  648.             MBSec = MBSAr.Sum() / TestIterations;
  649.             GBSec = MBSec / 1000;
  650.             Console.WriteLine("ClearFloatFrameArrayFill:              MS:" + ParseDouble(MsDuration) + " GB/s:" + ParseDouble(GBSec) + " MB/s:" + ParseDouble(MBSec));
  651.             MSAr.Clear();
  652.             MBSAr.Clear();
  653.  
  654.  
  655.  
  656.             Console.ReadLine();
  657.         }
  658.         static double ClearByteFrameAsSpanFill(byte clearValue) {
  659.             ByteFrame.AsSpan().Fill(clearValue);
  660.             return (float)ByteFrame.Length / 1000000;
  661.         }
  662.         static double ClearByteFrameArrayFill(byte clearValue) {
  663.             Array.Fill(ByteFrame, clearValue);
  664.             return (float)ByteFrame.Length / 1000000;
  665.         }
  666.         static double ClearByteFrameAsByteAVX(byte clearValue) {
  667.             fixed (byte* ByteFramePTR = ByteFrame) {
  668.                 byte* ByteFramePTRIncrementor = (byte*)ByteFramePTR;
  669.                 byte[] FillAr = new byte[32];
  670.                 FillAr.AsSpan().Fill(clearValue);
  671.                 fixed (byte* FillArPTR = FillAr) {
  672.                     Vector256<byte> FillVector = Avx2.LoadVector256(FillArPTR);
  673.                     //int remainder = ByteFrame.Length % 32;
  674.                     for (int i = 0; i <= ByteFrame.Length - 32; i += 32) {
  675.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  676.                         ByteFramePTRIncrementor += 32;
  677.                     }
  678.                 }
  679.             }
  680.             return (float)ByteFrame.Length / 1000000;
  681.         }
  682.         static double ClearByteFrameAsByteAVXUnRolled(byte clearValue) {
  683.             fixed (byte* ByteFramePTR = ByteFrame) {
  684.                 byte* ByteFramePTRIncrementor = (byte*)ByteFramePTR;
  685.                 byte[] FillAr = new byte[32];
  686.                 FillAr.AsSpan().Fill(clearValue);
  687.                 fixed (byte* FillArPTR = FillAr) {
  688.                     Vector256<byte> FillVector = Avx2.LoadVector256(FillArPTR);
  689.                     //int remainder = ByteFrame.Length % 32;
  690.                     for (int i = 0; i <= ByteFrame.Length - 96; i += 96) {
  691.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  692.                         ByteFramePTRIncrementor += 32;
  693.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  694.                         ByteFramePTRIncrementor += 32;
  695.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  696.                         ByteFramePTRIncrementor += 32;
  697.                     }
  698.                 }
  699.             }
  700.             return (float)ByteFrame.Length / 1000000;
  701.         }
  702.         static double ClearByteFrameAsByteAVXThreaded(byte clearValue, int numThreads) {
  703.             int byteFrameLength = ByteFrame.Length;
  704.             int amountPerThread = byteFrameLength / numThreads;
  705.  
  706.             CountdownEvent countdown = new CountdownEvent(numThreads);
  707.             for (int i = 0; i < numThreads; i++) {
  708.                 ThreadPool.QueueUserWorkItem(new WaitCallback(ClearByteFrameAsByteAvxThreadedDo), new object[] { i * amountPerThread, amountPerThread, countdown, clearValue });
  709.             }
  710.             countdown.Wait();
  711.             return (float)ByteFrame.Length / 1000000;
  712.         }
  713.         static private void ClearByteFrameAsByteAvxThreadedDo(object state) {
  714.             object[] array = state as object[];
  715.             int from = Convert.ToInt32(array[0]);
  716.             int amount = Convert.ToInt32(array[1]);
  717.             CountdownEvent countdown = (CountdownEvent)array[2];
  718.             byte clearValue = Convert.ToByte(array[3]);
  719.             int test = Thread.CurrentThread.ManagedThreadId;
  720.  
  721.             fixed (byte* ByteFramePTR = ByteFrame) {
  722.                 byte* ByteFramePTRIncrementor = (byte*)ByteFramePTR;
  723.                 byte[] FillAr = new byte[32];
  724.                 FillAr.AsSpan().Fill(clearValue);
  725.                 fixed (byte* FillArPTR = FillAr) {
  726.                     Vector256<byte> FillVector = Avx2.LoadVector256(FillArPTR);
  727.                     ByteFramePTRIncrementor += from;
  728.                     for (int i = from; i <= from+amount - 96; i += 96) {
  729.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  730.                         ByteFramePTRIncrementor += 32;
  731.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  732.                         ByteFramePTRIncrementor += 32;
  733.                         Avx2.Store(ByteFramePTRIncrementor, FillVector);
  734.                         ByteFramePTRIncrementor += 32;
  735.                     }
  736.                 }
  737.             }
  738.             countdown.Signal();
  739.         }
  740.         static double ClearByteFrameMarshalCopy(byte clearValue) {
  741.             fixed (byte* f = ByteFrame) {
  742.                 Marshal.Copy(ByteFrameClearer, 0, (nint)f, ByteFrameClearer.Length);
  743.             }
  744.             return (float)ByteFrame.Length / 1000000;
  745.         }
  746.         static double ClearByteFrameMarshalCopy4(byte clearValue) {
  747.             fixed (byte* f = ByteFrame) {
  748.                 byte* p = (byte*)f;
  749.                 int iterations = 4;
  750.                 int iterationlength = ByteFrame.Length / iterations;
  751.                 for (int i = 0; i < iterations; i++) {
  752.                     p = f + (i*iterationlength);
  753.                     Marshal.Copy(ByteFrameClearer, i * iterationlength, (nint)p, iterationlength);
  754.                 }
  755.             }
  756.             return (float)ByteFrame.Length / 1000000;
  757.         }
  758.         static double ClearByteFrameMarshalThreaded(byte clearValue, int numThreads) {
  759.             int byteFrameLength = ByteFrame.Length;
  760.             int amountPerThread = byteFrameLength / numThreads;
  761.  
  762.             CountdownEvent countdown = new CountdownEvent(numThreads);
  763.             for (int i = 0; i < numThreads; i++) {
  764.                 ThreadPool.QueueUserWorkItem(new WaitCallback(ClearBackBufferMarshalThreadedDo), new object[] { i * amountPerThread, amountPerThread, countdown });
  765.             }
  766.             countdown.Wait();
  767.             return (float)ByteFrame.Length / 1000000;
  768.         }
  769.         static private void ClearBackBufferMarshalThreadedDo(object state) {
  770.             object[] array = state as object[];
  771.             int from = Convert.ToInt32(array[0]);
  772.             int amount = Convert.ToInt32(array[1]);
  773.             CountdownEvent countdown = (CountdownEvent)array[2];
  774.             int test = Thread.CurrentThread.ManagedThreadId;
  775.  
  776.             bool debug = true;
  777.             fixed (byte* f = ByteFrame) {
  778.                 byte* p = (byte*)f;
  779.                 p = f + from;
  780.                 Marshal.Copy(ByteFrameClearer, from, (nint)p, amount);
  781.             }
  782.             countdown.Signal();
  783.         }
  784.         static double ClearByteFrameNaive(byte clearValue) {
  785.             for (int i = 0; i < ByteFrame.Length; i++) {
  786.                 ByteFrame[i] = clearValue;
  787.             }
  788.             return (float)ByteFrame.Length / 1000000;
  789.         }
  790.         static double ClearByteFrameBytePointer(byte clearValue) {
  791.  
  792.             fixed (byte* f = ByteFrame) {
  793.                 int x = 0;
  794.                 byte* p = (byte*)f;
  795.                 int maxloop = ByteFrame.Length - 32;
  796.                 for (; x < maxloop; x += 32) {
  797.                     *p++ = clearValue;
  798.                     *p++ = clearValue;
  799.                     *p++ = clearValue;
  800.                     *p++ = clearValue;
  801.                     *p++ = clearValue;
  802.                     *p++ = clearValue;
  803.                     *p++ = clearValue;
  804.                     *p++ = clearValue;
  805.                     *p++ = clearValue;
  806.                     *p++ = clearValue;
  807.                     *p++ = clearValue;
  808.                     *p++ = clearValue;
  809.                     *p++ = clearValue;
  810.                     *p++ = clearValue;
  811.                     *p++ = clearValue;
  812.                     *p++ = clearValue;
  813.                     *p++ = clearValue;
  814.                     *p++ = clearValue;
  815.                     *p++ = clearValue;
  816.                     *p++ = clearValue;
  817.                     *p++ = clearValue;
  818.                     *p++ = clearValue;
  819.                     *p++ = clearValue;
  820.                     *p++ = clearValue;
  821.                     *p++ = clearValue;
  822.                     *p++ = clearValue;
  823.                     *p++ = clearValue;
  824.                     *p++ = clearValue;
  825.                     *p++ = clearValue;
  826.                     *p++ = clearValue;
  827.                     *p++ = clearValue;
  828.                     *p++ = clearValue;
  829.                 }
  830.                 maxloop = ByteFrame.Length - 16;
  831.                 for (; x < maxloop; x += 16) {
  832.                     *p++ = clearValue;
  833.                     *p++ = clearValue;
  834.                     *p++ = clearValue;
  835.                     *p++ = clearValue;
  836.                     *p++ = clearValue;
  837.                     *p++ = clearValue;
  838.                     *p++ = clearValue;
  839.                     *p++ = clearValue;
  840.                     *p++ = clearValue;
  841.                     *p++ = clearValue;
  842.                     *p++ = clearValue;
  843.                     *p++ = clearValue;
  844.                     *p++ = clearValue;
  845.                     *p++ = clearValue;
  846.                     *p++ = clearValue;
  847.                     *p++ = clearValue;
  848.                 }
  849.                 maxloop = ByteFrame.Length;
  850.                 for (; x < maxloop; x += 1) {
  851.                     *p++ = clearValue;
  852.                 }
  853.             }
  854.  
  855.             return (float)ByteFrame.Length / 1000000;
  856.         }
  857.         static double ClearByteFrameInt32Pointer(Int32 bgra) {
  858.  
  859.             //Int32 bgra = (clearValue << 24) + (clearValue << 16) + (clearValue << 8) + clearValue;
  860.             fixed (byte* f = ByteFrame) {
  861.                 int x = 0;
  862.                 Int32* p = (Int32*)f;
  863.                 int maxloop = ByteFrame.Length - 128;
  864.                 for (; x < maxloop; x += 128) {
  865.                     *p++ = bgra;
  866.                     *p++ = bgra;
  867.                     *p++ = bgra;
  868.                     *p++ = bgra;
  869.                     *p++ = bgra;
  870.                     *p++ = bgra;
  871.                     *p++ = bgra;
  872.                     *p++ = bgra;
  873.                     *p++ = bgra;
  874.                     *p++ = bgra;
  875.                     *p++ = bgra;
  876.                     *p++ = bgra;
  877.                     *p++ = bgra;
  878.                     *p++ = bgra;
  879.                     *p++ = bgra;
  880.                     *p++ = bgra;
  881.                     *p++ = bgra;
  882.                     *p++ = bgra;
  883.                     *p++ = bgra;
  884.                     *p++ = bgra;
  885.                     *p++ = bgra;
  886.                     *p++ = bgra;
  887.                     *p++ = bgra;
  888.                     *p++ = bgra;
  889.                     *p++ = bgra;
  890.                     *p++ = bgra;
  891.                     *p++ = bgra;
  892.                     *p++ = bgra;
  893.                     *p++ = bgra;
  894.                     *p++ = bgra;
  895.                     *p++ = bgra;
  896.                     *p++ = bgra;
  897.                 }
  898.                 maxloop = ByteFrame.Length - 64;
  899.                 for (; x < maxloop; x += 64) {
  900.                     *p++ = bgra;
  901.                     *p++ = bgra;
  902.                     *p++ = bgra;
  903.                     *p++ = bgra;
  904.                     *p++ = bgra;
  905.                     *p++ = bgra;
  906.                     *p++ = bgra;
  907.                     *p++ = bgra;
  908.                     *p++ = bgra;
  909.                     *p++ = bgra;
  910.                     *p++ = bgra;
  911.                     *p++ = bgra;
  912.                     *p++ = bgra;
  913.                     *p++ = bgra;
  914.                     *p++ = bgra;
  915.                     *p++ = bgra;
  916.                 }
  917.                 maxloop = ByteFrame.Length - 4;
  918.                 for (; x < maxloop; x += 4) {
  919.                     *p++ = bgra;
  920.                 }
  921.             }
  922.  
  923.             return (float)ByteFrame.Length / 1000000;
  924.         }
  925.         static double ClearByteFrameInt32PointerThreads(Int32 bgra, int numThreads) {
  926.             /*Int32[] bgraAr = new Int32[3];
  927.             bgraAr[0] = (255 << 24) + (255 << 16) + (0 << 8) + 0;
  928.             bgraAr[1] = (255 << 24) + (0 << 16) + (255 << 8) + 0;
  929.             bgraAr[2] = (255 << 24) + (0 << 16) + (0 << 8) + 255;*/
  930.             if (ByteFrame.Length < numThreads) {
  931.                 throw new Exception("buffer length < numthreads");
  932.             }
  933.             int backBufferLength = ByteFrame.Length / 4;
  934.             int amountPerThread = backBufferLength / numThreads;
  935.  
  936.             CountdownEvent countdown = new CountdownEvent(numThreads);
  937.             for (int i = 0; i < numThreads; i++) {
  938.                 ThreadPool.QueueUserWorkItem(new WaitCallback(ClearBackBufferFromAmountInt32), new object[] { i * amountPerThread, amountPerThread, countdown, bgra });
  939.             }
  940.             countdown.Wait();
  941.  
  942.             return (float)ByteFrame.Length / 1000000;
  943.         }
  944.         static private void ClearBackBufferFromAmountInt32(object state) {
  945.             object[] array = state as object[];
  946.             int from = Convert.ToInt32(array[0]);
  947.             int amount = Convert.ToInt32(array[1]);
  948.             CountdownEvent countdown = (CountdownEvent)array[2];
  949.             Int32 bgra = Convert.ToInt32(array[3]);
  950.             int test = Thread.CurrentThread.ManagedThreadId;
  951.             bool debug = true;
  952.             fixed (byte* f = ByteFrame) {
  953.                 int x = 0;
  954.                 Int32* p = (Int32*)f;
  955.                 p += from;
  956.                 int maxloop = amount - 32;
  957.                 for (; x < maxloop; x += 32) {
  958.                     *p++ = bgra;
  959.                     *p++ = bgra;
  960.                     *p++ = bgra;
  961.                     *p++ = bgra;
  962.                     *p++ = bgra;
  963.                     *p++ = bgra;
  964.                     *p++ = bgra;
  965.                     *p++ = bgra;
  966.                     *p++ = bgra;
  967.                     *p++ = bgra;
  968.                     *p++ = bgra;
  969.                     *p++ = bgra;
  970.                     *p++ = bgra;
  971.                     *p++ = bgra;
  972.                     *p++ = bgra;
  973.                     *p++ = bgra;
  974.                     *p++ = bgra;
  975.                     *p++ = bgra;
  976.                     *p++ = bgra;
  977.                     *p++ = bgra;
  978.                     *p++ = bgra;
  979.                     *p++ = bgra;
  980.                     *p++ = bgra;
  981.                     *p++ = bgra;
  982.                     *p++ = bgra;
  983.                     *p++ = bgra;
  984.                     *p++ = bgra;
  985.                     *p++ = bgra;
  986.                     *p++ = bgra;
  987.                     *p++ = bgra;
  988.                     *p++ = bgra;
  989.                     *p++ = bgra;
  990.                 }
  991.                 maxloop = amount - 16;
  992.                 for (; x < maxloop; x += 16) {
  993.                     *p++ = bgra;
  994.                     *p++ = bgra;
  995.                     *p++ = bgra;
  996.                     *p++ = bgra;
  997.                     *p++ = bgra;
  998.                     *p++ = bgra;
  999.                     *p++ = bgra;
  1000.                     *p++ = bgra;
  1001.                     *p++ = bgra;
  1002.                     *p++ = bgra;
  1003.                     *p++ = bgra;
  1004.                     *p++ = bgra;
  1005.                     *p++ = bgra;
  1006.                     *p++ = bgra;
  1007.                     *p++ = bgra;
  1008.                     *p++ = bgra;
  1009.                 }
  1010.                 maxloop = amount;
  1011.                 for (; x < maxloop; x += 1) {
  1012.                     *p++ = bgra;
  1013.                 }
  1014.             }
  1015.             countdown.Signal();
  1016.         }
  1017.         static double ClearByteFrameInt64Pointer(Int64 bgra) {
  1018.  
  1019.             //Int64 bgra = (clearValue << 24) + (clearValue << 16) + (clearValue << 8) + clearValue;
  1020.             fixed (byte* f = ByteFrame) {
  1021.                 int x = 0;
  1022.                 Int64* p = (Int64*)f;
  1023.                 int maxloop = ByteFrame.Length - 256;
  1024.                 for (; x < maxloop; x += 256) {
  1025.                     *p++ = bgra;
  1026.                     *p++ = bgra;
  1027.                     *p++ = bgra;
  1028.                     *p++ = bgra;
  1029.                     *p++ = bgra;
  1030.                     *p++ = bgra;
  1031.                     *p++ = bgra;
  1032.                     *p++ = bgra;
  1033.                     *p++ = bgra;
  1034.                     *p++ = bgra;
  1035.                     *p++ = bgra;
  1036.                     *p++ = bgra;
  1037.                     *p++ = bgra;
  1038.                     *p++ = bgra;
  1039.                     *p++ = bgra;
  1040.                     *p++ = bgra;
  1041.                     *p++ = bgra;
  1042.                     *p++ = bgra;
  1043.                     *p++ = bgra;
  1044.                     *p++ = bgra;
  1045.                     *p++ = bgra;
  1046.                     *p++ = bgra;
  1047.                     *p++ = bgra;
  1048.                     *p++ = bgra;
  1049.                     *p++ = bgra;
  1050.                     *p++ = bgra;
  1051.                     *p++ = bgra;
  1052.                     *p++ = bgra;
  1053.                     *p++ = bgra;
  1054.                     *p++ = bgra;
  1055.                     *p++ = bgra;
  1056.                     *p++ = bgra;
  1057.                 }
  1058.                 maxloop = ByteFrame.Length - 128;
  1059.                 for (; x < maxloop; x += 128) {
  1060.                     *p++ = bgra;
  1061.                     *p++ = bgra;
  1062.                     *p++ = bgra;
  1063.                     *p++ = bgra;
  1064.                     *p++ = bgra;
  1065.                     *p++ = bgra;
  1066.                     *p++ = bgra;
  1067.                     *p++ = bgra;
  1068.                     *p++ = bgra;
  1069.                     *p++ = bgra;
  1070.                     *p++ = bgra;
  1071.                     *p++ = bgra;
  1072.                     *p++ = bgra;
  1073.                     *p++ = bgra;
  1074.                     *p++ = bgra;
  1075.                     *p++ = bgra;
  1076.                 }
  1077.                 maxloop = ByteFrame.Length - 8;
  1078.                 for (; x < maxloop; x += 8) {
  1079.                     *p++ = bgra;
  1080.                 }
  1081.             }
  1082.  
  1083.             return (float)ByteFrame.Length / 1000000;
  1084.         }
  1085.         static double ClearByteFrameInt64PointerThreads(Int64 bgra, int numThreads) {
  1086.             /*Int64[] bgraAr = new Int64[3];
  1087.             bgraAr[0] = (255 << 24) + (255 << 16) + (0 << 8) + 0;
  1088.             bgraAr[1] = (255 << 24) + (0 << 16) + (255 << 8) + 0;
  1089.             bgraAr[2] = (255 << 24) + (0 << 16) + (0 << 8) + 255;*/
  1090.             if (ByteFrame.Length < numThreads) {
  1091.                 throw new Exception("buffer length < numthreads");
  1092.             }
  1093.             int backBufferLength = ByteFrame.Length / 8;
  1094.             int amountPerThread = backBufferLength / numThreads;
  1095.  
  1096.             CountdownEvent countdown = new CountdownEvent(numThreads);
  1097.             for (int i = 0; i < numThreads; i++) {
  1098.                 ThreadPool.QueueUserWorkItem(new WaitCallback(ClearBackBufferFromAmountInt64), new object[] { i * amountPerThread, amountPerThread, countdown, bgra });
  1099.             }
  1100.             countdown.Wait();
  1101.  
  1102.             return (float)ByteFrame.Length / 1000000;
  1103.         }
  1104.         static private void ClearBackBufferFromAmountInt64(object state) {
  1105.             object[] array = state as object[];
  1106.             int from = Convert.ToInt32(array[0]);
  1107.             int amount = Convert.ToInt32(array[1]);
  1108.             CountdownEvent countdown = (CountdownEvent)array[2];
  1109.             Int64 bgra = Convert.ToInt64(array[3]);
  1110.             int test = Thread.CurrentThread.ManagedThreadId;
  1111.             bool debug = true;
  1112.             fixed (byte* f = ByteFrame) {
  1113.                 int x = 0;
  1114.                 Int64* p = (Int64*)f;
  1115.                 p += from;
  1116.                 int maxloop = amount - 32;
  1117.                 for (; x < maxloop; x += 32) {
  1118.                     *p++ = bgra;
  1119.                     *p++ = bgra;
  1120.                     *p++ = bgra;
  1121.                     *p++ = bgra;
  1122.                     *p++ = bgra;
  1123.                     *p++ = bgra;
  1124.                     *p++ = bgra;
  1125.                     *p++ = bgra;
  1126.                     *p++ = bgra;
  1127.                     *p++ = bgra;
  1128.                     *p++ = bgra;
  1129.                     *p++ = bgra;
  1130.                     *p++ = bgra;
  1131.                     *p++ = bgra;
  1132.                     *p++ = bgra;
  1133.                     *p++ = bgra;
  1134.                     *p++ = bgra;
  1135.                     *p++ = bgra;
  1136.                     *p++ = bgra;
  1137.                     *p++ = bgra;
  1138.                     *p++ = bgra;
  1139.                     *p++ = bgra;
  1140.                     *p++ = bgra;
  1141.                     *p++ = bgra;
  1142.                     *p++ = bgra;
  1143.                     *p++ = bgra;
  1144.                     *p++ = bgra;
  1145.                     *p++ = bgra;
  1146.                     *p++ = bgra;
  1147.                     *p++ = bgra;
  1148.                     *p++ = bgra;
  1149.                     *p++ = bgra;
  1150.                 }
  1151.                 maxloop = amount - 16;
  1152.                 for (; x < maxloop; x += 16) {
  1153.                     *p++ = bgra;
  1154.                     *p++ = bgra;
  1155.                     *p++ = bgra;
  1156.                     *p++ = bgra;
  1157.                     *p++ = bgra;
  1158.                     *p++ = bgra;
  1159.                     *p++ = bgra;
  1160.                     *p++ = bgra;
  1161.                     *p++ = bgra;
  1162.                     *p++ = bgra;
  1163.                     *p++ = bgra;
  1164.                     *p++ = bgra;
  1165.                     *p++ = bgra;
  1166.                     *p++ = bgra;
  1167.                     *p++ = bgra;
  1168.                     *p++ = bgra;
  1169.                 }
  1170.                 maxloop = amount;
  1171.                 for (; x < maxloop; x += 1) {
  1172.                     *p++ = bgra;
  1173.                 }
  1174.             }
  1175.             countdown.Signal();
  1176.         }
  1177.  
  1178.  
  1179.  
  1180.  
  1181.         static double ClearInt32FrameAsSpanFill(Int32 clearValue) {
  1182.             Int32Frame.AsSpan().Fill(clearValue);
  1183.             return (float)(Int32Frame.Length * 4) / 1000000;
  1184.         }
  1185.         static double ClearInt32FrameArrayFill(Int32 clearValue) {
  1186.             Array.Fill(Int32Frame, clearValue);
  1187.             return (float)(Int32Frame.Length * 4) / 1000000;
  1188.         }
  1189.  
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.         static double ClearFloatFrameAsSpanFill(float clearValue) {
  1196.             FloatFrame.AsSpan().Fill(clearValue);
  1197.             return (float)(FloatFrame.Length * 4) / 1000000;
  1198.         }
  1199.         static double ClearFloatFrameArrayFill(float clearValue) {
  1200.             Array.Fill(FloatFrame, clearValue);
  1201.             return (float)(FloatFrame.Length * 4) / 1000000;
  1202.         }
  1203.  
  1204.  
  1205.  
  1206.  
  1207.  
  1208.  
  1209.  
  1210.         static void ClearCache() {
  1211.             ByteFrame.AsSpan().Fill((byte)255);
  1212.             Int32Frame.AsSpan().Fill((int)255);
  1213.             FloatFrame.AsSpan().Fill((float)255);
  1214.             int sum = 0;
  1215.             for (int i = 0; i < ResetCacheArray.Length; i++) {
  1216.                 sum += ResetCacheArray[i];
  1217.             }
  1218.         }
  1219.     }
  1220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement