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 erick_weil · Feb 17, 2014 at 10:11 PM · saveloadfloatconvertbyte

Conversion of float[] to byte[] is too slow, how to optimize this?

I am making a voxel game, and to save the voxel data, that is a float[,,] variable, I divide then in nine float[] variables( to avoid out of memory issue ), and call the function to convert this float[] to byte[]

here is these function:

 static function float1dprabyte (floataux : float[],zeros : int) {
 var tempo : int =System.Environment.TickCount;
 var intuando : int[]=new int[floataux.Length];
 var byteArray : byte[] = new byte[floataux.Length*4];
 for(var j=0;j<intuando.Length;j++)
 {
 intuando[j]= Mathf.FloorToInt(floataux[j]*(Mathf.Pow(10,zeros)));
 var bytinho : byte[] = new byte[4];
 bytinho =System.BitConverter.GetBytes(intuando[j]);
 byteArray[(j*4)] = bytinho[0];
 byteArray[(j*4)+1] = bytinho[1];
 byteArray[(j*4)+2] = bytinho[2];
 byteArray[(j*4)+3] = bytinho[3];
 }
 tempo -= System.Environment.TickCount;
 Debug.Log("float1dprabyte : "+tempo);
 return(byteArray);
 }
 
 


and to load then i use a reverse function:

 static function byteprafloat (meuint32: byte[],zeros : int) {
 var tempo : int = System.Environment.TickCount;
 var intuando : int[]=new int[meuint32.Length/4];
 var Mx : int = 300;
 var My : int = 120;
 var Mz : int = 300;
 var floataux : float[] = new float[Mx*My*Mz];
 for(var j=0;j<meuint32.Length-4;j+=4)
 {
 intuando[j/4] =System.BitConverter.ToInt32(meuint32,j);
 floataux[j/4] = (1.0*intuando[j/4])/(Mathf.Pow(10,zeros));    
 }
 tempo -= System.Environment.TickCount;
 Debug.Log(" byteprafloat : "+tempo);
 return(floataux);
 }


and this is too slow, 1000 milisseconds for convert a float[300*120*300] into a byte[300*120*300*4] and 3000 miliseconds to convert a byte[300*120*300*4] into a float[300*120*300], with 9 slices of these, to make the complete float[900,120,900] voxel data, the save/load is to much slow...

how I can optimize this?

Comment
Add comment · Show 2
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 MakeCodeNow · Feb 17, 2014 at 11:23 PM 0
Share

Why are you converting from float to byte in the first place? 4 bytes takes just as much memory as one float, so it seems like a lot of needless conversion.

Also, I think you can entirely skip the Pow and FloorToInt. If you want to turn floats to bytes, just use BitConverter.GetBytes and then BitConverter.ToSingle.

avatar image erick_weil · Feb 17, 2014 at 11:28 PM 0
Share

I need to save the data, using WriteAllBytes... and I dont know how to save floats direct....

but a float, is made by 32bits == 4 bytes; so,4 bytes ocuppies the same memory that a float

thanks! now elapses 300 miliseconds to load and 1100 to save

0 Replies

· Add your reply
  • Sort: 

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How change float to byte[] and then byte[] to float without accuracy lost? 1 Answer

Save and Load Questions 0 Answers

Null Reference Exception even after check 1 Answer

Create a terrain prefab? Saving a terrain? No? 1 Answer

nice Save but wrong load 0 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