We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › What uses less memory
Page Index Toggle Pages: 1
What uses less memory? (Read 803 times)
What uses less memory?
Feb 17th, 2010, 1:29am
 
5 one-dimensional arrays of mixed types (float and String) or 1 two-dimensional array of type String?

The two arrays have the same content ie:

Code:


String[] array1 = {"Hello", "Goodbye", "What's Up?"};
String[] array2 = {"Yes", "No", "Maybe"};
String[] array3 = {"Question", "Answer", "Unknown"};
float[] array4 = {0.0, 1.0, 2.0};
float[] array5 = {3.0, 4.0, 5.0};

String[][] array6 = {
{"Hello", "Yes", "Question", "0.0", "3.0"},
{"Goodbye", "No", "Answer", "1.0", "4.0"},
{"What's Up?", "Maybe", "Unknown", "2.0", "5.0"},
};

Re: What uses less memory?
Reply #1 - Feb 17th, 2010, 2:35am
 
I'd imagine the strings take up the same space no matter how you store them - in either case, they are arrays of characters.

The floats, however, would normally be stored as 32 bits. Storing them as strings like you have in array6, they'd take up 4 characters per float... which is also 32 bits (they all have 4 letters (don't forget the terminating character!)).

Mixing types is also not good. Play it save as store your floats as floats...

... or, if you REALLY want to save some space, save them as bytes whose values are 10 times what they are now... and divide them by 10.0 when you use them! Ha ha!
Re: What uses less memory?
Reply #2 - Feb 17th, 2010, 2:45am
 
quoting Jackson, one of the computer science gurus they teach you at university:

"Don't optimize! If you have to, do it as the last step, after you have designed the program properly"

In this case, array6 does not represent the problem correctly, so you should keep away from it.  

PS: don't take it from me, take it from the man:

http://www.jacksonworkbench.co.uk/stevefergspages/jackson_methods/EinsteinLecturingOnJackson.jpg
Re: What uses less memory?
Reply #3 - Feb 17th, 2010, 3:36am
 
Thanks for your responses. That makes a lot of sense to me so Ill stick with 5 one-dimensional arrays. This brings me to my next question which lives on a different thread:

Sorting two-dimensional arrays
http://processing.org/discourse/yabb2/num_1227806624.html#8

How would I sort 5 one-dimensional Arrays all to the 4th array ie:

from this
Code:

String[] array1 = {"Hello", "Goodbye", "What's Up?"};
String[] array2 = {"Yes", "No", "Maybe"};
String[] array3 = {"Question", "Answer", "Unknown"};
float[] array4 = {1.0, 3.0, 2.0};
float[] array5 = {3.0, 4.0, 5.0};

to this
Code:

String[] array1 = {"Hello", "What's Up?", "Goodbye"};
String[] array2 = {"Yes", "Maybe", "No"};
String[] array3 = {"Question", "Unknown", "Answer"};
float[] array4 = {1.0, 2.0, 3.0};
float[] array5 = {3.0, 5.0, 4.0};


I know I can sort the 4th array using the sort method, but how would I translate that sort out to the other arrays?
Re: What uses less memory?
Reply #4 - Feb 17th, 2010, 3:45am
 
Neither...
Well, first solution is the least ugly of the two.
But since all data are related (or seems to be, if I follow the array6 example), it is better to group them in the same unit.
For this, you can create a class:
Code:
class Stuff
{
String smallTalk;
String answer;
String topic;
float oneValue;
float anotherValue;

SomeInformation(String st, String a, String t, float ov, float av)
{
smallTalk = st;
answer = a;
topic = t;
oneValue = ov;
anotherValue = av;
}
}
Of course, you should find more significant class name and field names...
Then you initialize as:
Code:
Stuff[] info =
{
new Stuff("Hello", "Yes", "Question", 0.0, 3.0),
new Stuff("Goodbye", "No", "Answer", "1.0", "4.0"),
new Stuff("What's Up?", "Maybe", "Unknown", "2.0", "5.0")
};


Moreover, it maintains consistency, which is harder with independent arrays.
Thus, as I see your second question while I type this answer, you can sort the array according to whatever field you want, relation between data is preserved.
To sort an array of objects, you have to look into Comparable or Comparator classes (interfaces). Something raised from time to time in the forum, a quick search should give hints.
Re: What uses less memory?
Reply #5 - Feb 17th, 2010, 3:49am
 
!!!!! that's terrible 8)

use a class to keep all the information relating to one item together in the same place.

class Item {
 String name;
 String dunno;
 String str;
 float num1;
 float num2;
}

(use decent names for things, i can't work out what they are from your example)

then have an array of Items. then you can use standard java library methods to sort them.

this will probably take more space. why are you so concerned with the size anyway?

(edit: hi PhiLho)
Re: What uses less memory?
Reply #6 - Feb 17th, 2010, 5:44am
 
Thanks for the input. Im not sure why I was so reluctant to put it into a class. Ill do that. Im not too familiar with standard Java sorting methods, Java at all really. Thats why I use Processing, ha!

I did a quick search and found the hack for "Comparable," Ill give this a try and keep you guys posted.

===== Forgot the link
http://processing.org/hacks/hacks:comparable

I was concerned about size because Im using a couple other libraries and using a lot of particles to comprise this project Im working on. Just trying to save space where I can.
Re: What uses less memory?
Reply #7 - Feb 17th, 2010, 6:13am
 
Wow, like magic. Thanks guys!
Page Index Toggle Pages: 1