【发布时间】:2018-10-07 15:13:29
【问题描述】:
我有一个包含数百万字节的数组。这些字节是 int 值(Int16、Int24 或 Int32)。现在我想从一定数量的字节中获取具有最大 int 值的 x 字节。
为了更好地解释这一点,让我们想象一个包含 10 个条目的数组:
byte[] arr = {255, 10, 55, 60, 128, 90, 88, 66, 199, 56};
我会知道我们使用的是 In16、Int24 还是 Int32,所以对于这个示例,假设我们使用的是 Int16。这意味着,我们使用 2 个字节来表示一个 Int16。所以 Ints 包括:
{255, 10},
{55, 60},
{128, 90},
{88, 66},
{199, 56}
问题 1:因为这是音频处理所需要的,所以 1046 低于 -2096。所以有必要独立于消极性进行比较
问题2:因为这需要非常高效,将字节转换为 Ints 进行比较似乎效率低下,应该有其他方法。
这是起点:
/// <summary>
/// Gets the maximum value of a number of bytes representing Int-Values
/// </summary>
/// <returns>The channels.</returns>
/// <param name="leftChannel">Left channel.</param>
/// <param name="rightChannel">Right channel.</param>
/// <param name="bytesPerInt">Bytes per int. 2 bytes = Int16, 3 bytes = Int24, 4 bytes = Int32</param>
/// <param name="countBytesToCombine">The number of bytes to look for the highest value</param>
private (byte[] combinedLeft, byte[] combinedRight) CombineChannels(byte[] leftChannel, byte[] rightChannel, int bytesPerInt, int countBytesToCombine)
{
}
/// <summary>
/// Gets the highest byte[] value
/// </summary>
/// <returns>The highest value. The size of the byte array is equal the bytesPerInt</returns>
/// <param name="bytes">A subarray of the given byte array of the upper method. The size of this array is equals countBytesToCombine</param>
/// <param name="bytesPerInt">The count of bytes representing an Int</param>
private byte[] GetHighestValue(byte[] bytes, int bytesPerInt)
{
}
编辑2
这是一个可行的解决方案,但对于每个通道执行 1400 万字节大约需要 2 秒,这太远了。
/// <summary>
/// Gets the maximum value of a number of bytes representing Int-Values
/// </summary>
/// <returns>The channels.</returns>
/// <param name="leftChannel">Left channel.</param>
/// <param name="rightChannel">Right channel.</param>
/// <param name="bytesPerInt">Bytes per int. 2 bytes = Int16, 3 bytes = Int24, 4 bytes = Int32</param>
/// <param name="countValuesToCombine">The number of bytes to look for the highest value</param>
private (byte[] combinedLeft, byte[] combinedRight) CombineChannels(byte[] leftChannel, byte[] rightChannel, int bytesPerInt, int countValuesToCombine)
{
var cLeft = new List<byte>();
var cRight = new List<byte>();
for (int i = 0; i < leftChannel.Length; i += countValuesToCombine * bytesPerInt)
{
var arrLeft = SubArray(leftChannel, i, countValuesToCombine * bytesPerInt);
var arrRight = SubArray(rightChannel, i, countValuesToCombine * bytesPerInt);
cLeft.AddRange(GetHighestValue(arrLeft, bytesPerInt));
cRight.AddRange(GetHighestValue(arrRight, bytesPerInt));
}
return (cLeft.ToArray(), cRight.ToArray());
}
/// <summary>
/// Gets the highest byte[] value
/// </summary>
/// <returns>The highest value.</returns>
/// <param name="bytes">Bytes.</param>
/// <param name="bytesPerInt">The count of bytes representing an Int</param>
private byte[] GetHighestValue(byte[] bytes, int bytesPerInt)
{
byte[] bytesOfHighestValue = new byte[bytesPerInt];
for (int i = 0; i < bytes.Length; i += bytesPerInt)
{
var arr = SubArray(bytes, i, bytesPerInt);
if (IsValueHigher(arr, bytesOfHighestValue, bytesPerInt))
{
bytesOfHighestValue = arr;
}
}
return bytesOfHighestValue;
}
private bool IsValueHigher(byte[] one, byte[] two, int bytesPerInt)
{
var o = ConvertToInt(one, bytesPerInt);
var t = ConvertToInt(two, bytesPerInt);
return Math.Abs(o) > Math.Abs(t);
}
private int ConvertToInt(byte[] bytes, int bytesPerInt)
{
switch (bytesPerInt)
{
case 2:
return BitConverter.ToInt16(bytes, 0);
case 3:
return Int24.ToInt32(bytes, 0);
case 4:
return BitConverter.ToInt32(bytes, 0);
}
return 0;
}
这很难解释,所以请在投票前询问是否有问题。
【问题讨论】:
-
不应该
GetHighestValue例如返回一个 Int16 吗? -
这个方法是一个例子,将在
CombineChannels内部调用。如果没有性能滞后,这也可以返回 Int16 或其他。但我想,在转换和打开一个新的瓶颈之前,让我们返回代表这个 Int16 值的 2 个字节 -
如果你想返回 byte[] 那么它将是 byte[][] 或 byte[][] 的列表,例如 byte[10] 将变成 List
有五个项目。 -
我想我明白你的问题是什么,但我不知道你在问什么。您是否尝试过自己解决问题?还是您只是在问如何从性能角度解决问题?
-
我能够自己解决这个问题,但它的性能太差,我删除了代码,所以我需要一些帮助来使它在现实世界的场景中可用。所以是的,我需要性能方面的帮助。