rabbit (she/he) <plural>
rabbit (she/he) <plural>
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
namespace Xor;

public static class Xor
{
public static int[] Decode(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

for (int i = 1; i < arr.Length; i++)
{
first = arr[i] = encoded[i - 1] ^ first;
}

return arr;
}

public static int[] DecodeWithSpans(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

var encodedSpan = encoded.AsSpan();
var arrSpan = arr.AsSpan(1);
for (int i = 0; i < arrSpan.Length; i++)
{
first = arrSpan[i] = encodedSpan[i] ^ first;
}

return arr;
}
}
namespace Xor;

public static class Xor
{
public static int[] Decode(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

for (int i = 1; i < arr.Length; i++)
{
first = arr[i] = encoded[i - 1] ^ first;
}

return arr;
}

public static int[] DecodeWithSpans(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

var encodedSpan = encoded.AsSpan();
var arrSpan = arr.AsSpan(1);
for (int i = 0; i < arrSpan.Length; i++)
{
first = arrSpan[i] = encodedSpan[i] ^ first;
}

return arr;
}
}
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
| Method | N | Mean | Error | StdDev |
|---------------- |------ |-----------:|---------:|---------:|
| Decode | 1000 | 859.4 ns | 1.70 ns | 1.59 ns |
| DecodeWithSpans | 1000 | 843.9 ns | 2.06 ns | 1.72 ns |
| Decode | 10000 | 8,013.4 ns | 31.75 ns | 26.52 ns |
| DecodeWithSpans | 10000 | 7,857.6 ns | 13.34 ns | 10.41 ns |
| Method | N | Mean | Error | StdDev |
|---------------- |------ |-----------:|---------:|---------:|
| Decode | 1000 | 859.4 ns | 1.70 ns | 1.59 ns |
| DecodeWithSpans | 1000 | 843.9 ns | 2.06 ns | 1.72 ns |
| Decode | 10000 | 8,013.4 ns | 31.75 ns | 26.52 ns |
| DecodeWithSpans | 10000 | 7,857.6 ns | 13.34 ns | 10.41 ns |
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
thx all
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
i loop hoist myself instead of relying on the compiler to do it
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
i suppose copying is more expensive than having multiple indexes
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
this is my final submission:
public class Solution {
public int[] Decode(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

for (int i = 1; i < arr.Length; i++)
{
first = arr[i] = encoded[i-1] ^ first;
}

return arr;
}
}
public class Solution {
public int[] Decode(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

for (int i = 1; i < arr.Length; i++)
{
first = arr[i] = encoded[i-1] ^ first;
}

return arr;
}
}
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
/ can I output the IL code
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
also, it's not represented in my examples, but is previous element value cached with compiler optimization
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
i.e. System.Buffer.BlockCopy versus multiple array indexes in loop
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
just trying to understand from a theoretical standpoint rn
44 replies
CC#
Created by rabbit (she/he) <plural> on 5/2/2023 in #help
✅ Which of these for-loops is more performant? How can I improve
I will try that later yes
44 replies