RT,我从文件中解析出定长的数据(byte[400])的数据后为了防止后续操作出错要检查数据是否是全0状态,要怎么做,
应为该操作的频度极高,用循环来检查的话CPU负荷过高,不想用这种方法

解决方案 »

  1.   

    如果在读的时候使用ReadByte,则可以同步计算判断。否则,一个数组不循环每个元素能判断其全0吗?
      

  2.   

    用循环来检查, you have to.
      

  3.   

    And it is much faster than "从文件中解析出定长的数据"
      

  4.   

                  [DllImport("msvcrt.dll")]
                  private static extern int memcmp(byte[] b1, byte[] b2, int count);
     
                byte[] emtpyArray = new byte[400];
                emtpyArray.Initialize();            byte[] dataArray = new byte[400];
                //to do, fill data to data array.
                
                if(0 == memcmp(emtpyArray,dataArray,400))
                {
                    // 内容相同
                  }
                else 
                {
                    // 内容不同
                  }
      

  5.   

    我是是用这样的代码来解析文件的啊,如果改成 for+ReadByte的话,CPU肯定会上到60%以上吧 byte[] buffer = new byte[400];
    Data.Seek(0, SeekOrigin.Begin);//Data是MemoryStream类实体
    while (Data.Position < Data.Length)
    {
        Data.Read(buffer, 0, 400);
        OnSendData(buffer);//异步委托,将解析后的数据传递出去
    }
      

  6.   

    找到方法了,可以用Array.FindIndex<T>来做,按照谓词定义在检索数组中的值,如果是全0(没有找到 ==0的值),则返回-1,如果有任何一个byte >0,则返回一个>=0的索引值/// <summary>
    /// Array.FindIndex用谓词定义:大于0
    /// </summary>
    private bool NotZero(byte d)
    {
        if (d != 0)
            return true;
        else
            return false;
    }Array.FindIndex(buffer,NotZero) < 0