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
public class Solution {
public int[] DecodeOne(int[] encoded, int first)
{
var arr = new int[encoded.Length + 1];
arr[0] = first;

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

return arr;
}

public int[] DecodeTwo(int[] encoded, int first)
{
var intSize = sizeof(int);
var arr = new int[encoded.Length + 1];
arr[0] = first;
System.Buffer.BlockCopy(encoded, 0, arr, intSize, intSize * encoded.Length);

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

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

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

return arr;
}

public int[] DecodeTwo(int[] encoded, int first)
{
var intSize = sizeof(int);
var arr = new int[encoded.Length + 1];
arr[0] = first;
System.Buffer.BlockCopy(encoded, 0, arr, intSize, intSize * encoded.Length);

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

return arr;
}
}
Not sure if I'm thinking about this correctly or not. Would love to be pointed to resources regarding optimization if you have them. ~skr
44 replies