小编Osc*_*car的帖子

C#中最快的方法是从文件中读取字节块并转换为float []

我需要在C#leanguage中快速转换/转换字节数组,将2字节的一个short(int16)值编码为float表示,尽可能快.性能瓶颈是方法:

samples[sample] = (float)binraryReader.readInt16();

(巨大的IO调用,所以我不得不转换为块读取)

基本上我有包含声音样本块(~100-600 mb)类型的短文件,然后,因为我只能阻止读取字节集,我需要从每对字节构造短路然后将该短路转换为浮点数表示我需要将样本存储为浮点数.

我目前的代码看起来像这样(比上面的方法提高了2倍的性能,但仍然很长):

    float[] samples = new float[_samplesPerSplit];
    byte[] data = new byte[_samplesPerSplit * 2];

    for (int c = 0; c < numberOfChunks; c += 1)
    {
        br.Read(data, 0, _samplesPerSplit * 2);

        fixed (byte* bytePtr = data)
        {
            fixed (float* floatPtr = samples)
            {
                byte* rPos = bytePtr;
                float* fPos = floatPtr;

                byte byte0;
                byte byte1;
                short sampleShort;

                for (int sample = 0; sample < _samplesPerSplit; sample += 1)
                {
                    byte1 = *(rPos++);
                    byte0 = …
Run Code Online (Sandbox Code Playgroud)

c# io casting file type-conversion

5
推荐指数
1
解决办法
2084
查看次数

标签 统计

c# ×1

casting ×1

file ×1

io ×1

type-conversion ×1