Skip to main content
GameDev.net gamedev.net
🔒 Locked

[.net] CONVERT Class speed

Started by coolblue Jan 2, 2007 at 7:58 AM 5 replies 650+ views
Original Post
coolblue
coolblue
I have a binary number in the form of a string eg "1111" which I need to check (each 1 denotes whether a particular square has a property associated with a side, first 1 = top, second =right etc). Is it more efficient to use the convert.ToByte function and then check the number in that byte within a switch statement (eg 15 =all sides have property set) or would it be more efficient to simply do a substring on the string and check those?
joelmartinez
joelmartinez
I'll start off by saying that I don't know ... you'll probably have to write up a test and profile your code in order to find out. but I think the bigger question is, why is "1111" stored as a string?

If it's for something like loading a level from a file, then it probably won't matter much how fast it is since you're only going to do it once. Just go with the easier to program method.

If however it's something that you're doing/checking once per frame, then you should probably rethink how you're storing that data. Make a struct or class which holds the data so that no parsing has to occur at runtime.
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]
coolblue
coolblue
Very good point. I had not thought this through properly (if I had the answer would be quite obvious).

Yes I am loading this from a map file initially but then I will need to store it for use in collisions. So I guess it will be best for me to store this value as a byte and do the checks on the byte value.

Thanks for pointing me in the right direction.
TheTroll
TheTroll
Can I make a suggestion?

Don't use a set of 1s and 0s in a string for this. Use a number, say a short or int depending on how many bits you need.

Use the Int32.Parse(your_string) to convert your string back into an int.
Use ToString to convert your int back into a string.

Use bitwize operations to check if each bit is set for the appropriate property.

I think it would be much more effiecent then what you are thinking about.

theTroll
joelmartinez
joelmartinez
I'm not sure all that is needed ... since he's only doing it once when the file is loaded, it's better to just write the code that is easier to read and maintain. Something like:
        struct FileValues        {            public bool Top, Left, Right, Bottom;        }...        public FileValues Parse(string properties)        {            FileValues values;            values.Top = properties[0] == '1';            values.Right = properties[1] == '1';            values.Bottom = properties[2] == '1';            values.Left = properties[3] == '1';            return values;        }
Edit: and also, if you're going to use the .Parse method, use .TryParse instead ;-)
Joel Martinez
http://codecube.net
[twitter]joelmartinez[/twitter]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.