在 Unity 中读取文本比 C# 控制台应用程序慢很多

Reading from a text in Unity is a lot slower than C# console application

基于 问题,我有一个包含 7500 万行的这种格式的文本文件:

X Y Z colorvalues -0.680891 -90.6809 0 204 204 204 255

我正在从文件中读取,操作真的很慢。经过一番讨论后,我意识到只有在 Unity 上操作才慢。我测试了以下代码:

StreamReader sr;

// Use this for initialization
void Start () {
    sr = new StreamReader(Application.dataPath + "/PointCloud/DVS.txt");
    Debug.Log("Read is called");
    Read();
}

void Read()
{

    string line;
    while ((line = sr.ReadLine())!=null)
    {

    }
    Debug.Log("Read has ended");
}

只读取一个txt文件,不做任何其他操作,需要3分钟。当我使用 C# 应用程序或 C++ 对其进行测试时,最多需要 30 秒。我真的很好奇是什么导致了这个问题。如果有人能解释为什么这个问题发生在 Unity 上,我会很高兴。谢谢!

这是带有新线程的 Profiler 照片。

今天我弄清楚问题出在哪里了。我在编辑器和为调试而构建的开发中测试了这些代码。当我为 Windows 正确构建它时,阅读速度快了很多,并在大约 30 秒内阅读了所有行。