Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by u3d_loveye · Mar 31, 2011 at 02:48 AM · javascriptspeedslowreadtxt

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.....

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Justin Warner · Mar 31, 2011 at 03:01 AM 0
Share

How big is the text file? Good god... trying to make a small game or something? Or is Unity just slow at it?

avatar image u3d_loveye · Mar 31, 2011 at 03:09 AM 0
Share

it's not very big, about 500 $$anonymous$$B .

avatar image SirGive · Mar 31, 2011 at 06:01 AM 0
Share

500kb is huge! No wonder it takes so long

avatar image Proclyon · Mar 31, 2011 at 07:38 AM 0
Share

Aren't you supposed to close any form of I/O After opening? Or is that just me

avatar image Statement · Mar 31, 2011 at 12:07 PM 0
Share

500 kB isn't huge.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Flynn · Nov 28, 2012 at 07:09 AM 0
Share

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();


avatar image
1

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();

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Why is C# joystick taking 300 times slower to execute then javascript Joystick? 1 Answer

Third person controls: Character speed and animation dont match 1 Answer

obj location on txt file 1 Answer

puzzle with rotating statues 1 Answer

Unity Speed Issue on a Macbook 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges