首页 > 解决方案 > 优化,从内存中填充数据

问题描述

我正在尝试使用读取内存填充对象。我正在使用结构,它运行得非常快,至少看起来是这样。

float x = 0;

while (true)
{
    st.Start();

    List<DataChild> DataChildList = new List<DataChild>();
    var Data = Algorithm.Data;
    Data.ChildData.pData = IntPtr.Add(Data.ChildData.pData, 0x20);

    for (int i = 0; i < Data.NumberOfObjects; i++)
    {
        ChildData childData = Data.ChildDatas.ReadValue(i, true);

        if (childData.Render.X != x)
        {
            Console.WriteLine("Change detected");
            x = childData.Render.X;
                            }
            DataChildList.Add(childData)
        }

        Algorithm.Data.Datas = DataChildList;
        st.Stop();
        Console.WriteLine($"populated in {st.Elapsed.TotalMilliseconds} ms");

        st.Reset();
   }
}

有我的结构:

[StructLayout(LayoutKind.Explicit)]
public struct ChildData
{
  [FieldOffset(0x0)]
  public IntPtr BasePointer;

  [FieldOffset(0x400)]
  public IntPtr pRender;

  public Vector3 Render
  {
    get
    {
     return M.Read<Render>(this.pRender);
    }
  }
}

[StructLayout(LayoutKind.Explicit)]
public struct Render
{
 [FieldOffset(0x30)]
 public float X;

public float posX
{
  get
  {
   return this.X;
  }
}
}

秒表说我将数据填充到0.005 ms.

当我按下一个键时,它会更改childData.Render.X(目前列表中唯一的一个)的值。

但它会在 0.5 到 1 秒之间显示它,与循环速度相比,这似乎非常慢。

不得不说这段代码是跑到一个Thread中的。

关于为什么需要这么长时间的任何想法?谢谢!

标签: c#memorystruct

解决方案


推荐阅读