Sign in to follow this  
johnboy007

string compare or not to string compare?

Recommended Posts

johnboy007    122
Hi guys just wondering what to use instead of comparing strings, i have an array containing the names of objects that i need to compare against object names but i know this can be expensive, is there any other way? btw i'm using javascript

Share this post


Link to post
Share on other sites
ryan20fun    2635
can you enumerate it ?
in C++ and C# (VB.net and such do it as well i think)
[code]
enum ENUMERATE
{
enum1,
enum2,
enum3
//etc
};

// just coneptual code
class whaterver
{
ENUMERATE e = ENUMERATE.enum1;
if(e == ENUMERATE.enum2)
{
}
}
[/code]

Share this post


Link to post
Share on other sites
FelixK15    335
Why enumeration?

If he want's a unique identifier per object I don't see the reason why he's not using integer as identifier.

Share this post


Link to post
Share on other sites
rljohn    103
[quote name='FelixK15' timestamp='1307365396' post='4820056']
Why enumeration?

If he want's a unique identifier per object I don't see the reason why he's not using integer as identifier.
[/quote]


Readability, typically.

Share this post


Link to post
Share on other sites
FelixK15    335
[quote name='rljohn' timestamp='1307369689' post='4820087']
Readability, typically.


[/quote]

We are talking about UNIQUE identifiers!
Every object created has a different identifier.

How do you want to realize that with enums?

[code]
Object *o1 = new Object();
Object *o2 = new Object();
Object *o3 = new Object();

std::cout << o1->identifier; // 1
std::cout << o2->identifier; // 2
std::cout << o3->identifier; // 3
[/code]

Share this post


Link to post
Share on other sites
way2lazy2care    790
[quote name='FelixK15' timestamp='1307372490' post='4820097']
[quote name='rljohn' timestamp='1307369689' post='4820087']
Readability, typically.


[/quote]

We are talking about UNIQUE identifiers!
Every object created has a different identifier.

How do you want to realize that with enums?

[code]
Object *o1 = new Object();
Object *o2 = new Object();
Object *o3 = new Object();

std::cout << o1->identifier; // 1
std::cout << o2->identifier; // 2
std::cout << o3->identifier; // 3
[/code]
[/quote]
I think the OP is a little fuzzy. It depends both on the need and the number/states of the objects you are using them for.

Enum or int is definitely better than comparing strings though for sure.

Share this post


Link to post
Share on other sites
FelixK15    335
Sorry, but I don't see the sense in using enums as unique identifier.

If he wants to classifiy the type of object, enums are great.
Example:

[code]

switch(object->type){
case GAMEOBJECT:
//Do Stuff
break;

case MOVABLEOBJECT:
//Do Stuff
break;

case RENDERABLEOBJECT:
//Do Stuff
break;
}

[/code]

Thats what you should use enums for IMHO.

Share this post


Link to post
Share on other sites
Part_E    100
As other's have stated an ID member is probably the way to go. If you're concerned about overhead, and you're definitely not tracking more than 256 objects, you can just store the ID as an unsigned char.

Are these definitely objects WITH strings that you're storing though, or just the strings themselves?
If just the strings themselves, consider instead an object class with and ID and string member.
Then you could invoke a custom constructor to handle ID assignment and methods for ID comparison.

Although I'm not sure why you need to compare lists of strings in the first place... Were you using the strings as a sort of ID and nothing more, or do you actually need the string data?

Share this post


Link to post
Share on other sites
Vectorian    109
For javascript, the overhead of string comparisons is negligible. And even if that was not the case, it's still blazingly fast, are you doing billions per second? Else I would not worry.

Share this post


Link to post
Share on other sites
Part_E    100
Oh, this is a scripting language. Guess I should've re-read the OP. Yeah, if this is just a scripting language you should be able to throw caution to the wind and just compare whatever you like.
If it ends up running slowly, well that's the interpreter's problem ;)

Share this post


Link to post
Share on other sites
wqking    761
Comparing string when performance doesn't matter. Such like you compare 10 strings each frame in the main menu.
Use hash or unique ID (an integer) to represent string and compare it when performance does matter.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this