• 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 pickledzebra · Jul 05, 2010 at 03:54 PM · textfilereadoptimize

Optimize the reading of a text file?

I've followed a lot of suggestions for optimizing graphics performance, and my projects currently sails along quickly and lightly in terms of rendering.

I do, however, have some overhead on loading that I'd like to minimize if possible. It comes down to reading a text file of objects and coordinates (this approach is questionable, but let's stick with it as a given).

I'm reading in a file that contains a subset of lines I'm interested in ("MARK" being the intial characters). Then, only a subset of these lines, I want to store coordinate information for - I check for the existence of the line type using a single character from the line string in a hash I populate at the start of the code.

I'm using builtin arrays for everything below.

I have the following:

if (textAsset == null) return; reader = new StringReader(textAsset.text); line = reader.ReadLine();

while (line != null) {

 if(line.Substring(0,4) == "MARK")
 {

     var identifier = Regex.Replace(line.Substring(13,4),"( )+","");

     if (lineType[identifier])
     {
         i++;

         totalCounts++;


 vec[i] = Vector3(float.Parse(line.Substring(30,8)),float.Parse(line.Substring(38,8)),float.Parse(line.Substring(46,8)));
 vecSum += Vector3(float.Parse(line.Substring(30,8)),float.Parse(line.Substring(38,8)),float.Parse(line.Substring(46,8)));

         print(totalCounts);
         print(DateTime.Now);

     }
 }

}

I'm clocking about 200 lines read per second. I'm wondering if anyone sees anything in the above code that could be streamlined for a time gain?

Cheers, and thanks for reading this far!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Mike 3 · Jul 05, 2010 at 04:16 PM

First thing i see - you're parsing the vectors twice each - store the intermediary vector, then use it for vec[i] and adding to vecSum

Second, you really should be using the overload of float.Parse that takes a CultureInfo object - otherwise on european computers, it'll have problems reading US/UK floats (and vice versa)

Third, you're printing two items each time - that'll slow you down ridiculously (it has to print the current stacktrace to a file, as well as displaying it in the console), so remove those if you're testing performance

Comment
Add comment · Show 4 · 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 pickledzebra · Jul 05, 2010 at 04:27 PM 0
Share

Thanks for the pointer on #2. #3 is just temporary while I figure this out.

avatar image pickledzebra · Jul 05, 2010 at 04:35 PM 0
Share

Hmmm... fixed the #1 and deleted #3. Neither reduces the time outside the variance of total time from run to run.

avatar image pickledzebra · Jul 05, 2010 at 04:43 PM 0
Share

I think parsing the input text file down will get me to where I need to go. I was hoping the readin would be sufficiently fast that I could ignore large chunks of non-interesting data in my input files. Thanks for looking, mike.

avatar image Mike 3 · Jul 05, 2010 at 05:05 PM 0
Share

I'm assu$$anonymous$$g that you've skipped the bit of code which stops the above being an infinite loop though - line never gets modified, so it'll just hang. Also - I'm not sure if reading line by line will be faster than reading once then splitting, if you don't need to break out early

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Why is reading some text files with JS so darn slow?! (title changed to reflect answers) 1 Answer

Write and read text files with c# 1 Answer

How do I read read from and write to a text file? 2 Answers

Problem Reading A Text File 2 Answers

Best way to manage stats in a text file 2 Answers


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