- Home /
how to read a txt file faster ? my projects' loading time is so long ! about 30 minutes!!!
the codes to read txt file and parse out the datas i need:
#pragma strict import System.IO; import System.Text; var stream:FileStream; var str : String ; ////many other var ....
function Start() {
if (str== "" ) { ///reading txt file>......... stream = File.Open("g:\\Data.txt",FileMode.Open); var b :byte[] = new byte [8]; var temp:UTF8Encoding = new UTF8Encoding(true); while(stream.Read(b,0,b.Length)>0) str += temp.GetString(b); }
///////parse out the datas i want ......
var rows : String[] = str.Split("$"[0]); var arr = new Array(); var arra=new Array(); for ( var i:int=1;i<rows.length-1; i++ ) { var datas : String[] = rows[i].Split(","[0]);
var x1 = ParseStr(datas[3]);
var z1 = ParseStr(datas[5]);
var x2 = datas[12];//
var y2 = datas[8];//
var z2 = datas[13];//
var w2= datas[7];//
var v4:Vector4;//
var v5:Vector4;//
v4.x = x1;
v4.y = float.Parse("0");
v4.z = z1;
v4.w=speed;//w
v5.x=float.Parse(x2);//0;//
v5.y=float.Parse(y2);//
v5.z=float.Parse(z2);//0;//
v5.w=float.Parse(w2);//
arr.Push(v4);
arra.Push(v5);
} MoveDatas = new Vector4[arr.length]; MoveDatass = new Vector4[arra.length];
for ( var j:int=0;j<arr.length; j++ ) { MoveDatas[j] =arr[j]; MoveDatass[j]=arra[j]; }
StartMove(); }
so how can i improve my codes to make it run faster? someone help me please.....
How big is the text file? Good god... trying to make a small game or something? Or is Unity just slow at it?
Aren't you supposed to close any form of I/O After opening? Or is that just me
Answer by Harald Scheirich · Mar 31, 2011 at 11:52 AM
You are already starting of slow by reading your file 8 bytes at a time. By using a TextAsset you should be able to speed up the actual loading. This will also make your application more portable.
Reducing the number of temporary variables will also speed up your code.
As for the parsing it looks like you are converting a lot of numbers from string to floats, if after step one you are still slow you might have to move away from a readable text format and use a binary format that reads the numbers directly.
In general this piece on data handling should help you out a little bit more.
Great answer!
I would recommend StringBuilders too! (They are much faster than using str+=) Something along the lines of:
System.Text.StringBuilder SB = new System.Text.StringBuilder();
for(...)
{
...
SB.Append(temp.GetString(b));
}
...
str = SB.ToString();
Answer by Statement · Mar 31, 2011 at 12:12 PM
How about this? What is ParseStr function?
import System.Collections.Generic;
function Start() {
var file : String = File.ReadAllText("g:\\Data.txt"); var rows : List.<String> = List.<String>(file.Split("$"[0])); rows.RemoveAt(0); rows.RemoveAt(rows.Count - 1); var v1List = List.<Vector4>(rows.Length); var v2List = List.<Vector4>(rows.Length);
for (var row : String in rows) { var datas : String[] = row.Split(","[0]);
var v1 : Vector4;
v1.x = ParseStr(datas[3]);
v1.y = 0;
v1.z = ParseStr(datas[5]);
v1.w = speed;
var v2 : Vector4;
v2.x = float.Parse(datas[12]);
v2.y = float.Parse(datas[8]);
v2.z = float.Parse(datas[13]);
v2.w = float.Parse(datas[7]);
v1List.Add(v1);
v2List.Add(v2);
}
// I guess these are some member variables of sorts.
MoveDatas = v1List.ToArray();
MoveDatass = v2List.ToArray()
StartMove();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613115859im_/https://answers.unity.com/themes/thub/images/avi.jpg)