- Home /
What's faster to compare, strings or typeof(s) ?
What's faster, comparing 2 strings (pre-saved strings representing the names of the objects for example), or comparing a typeof their objects?
In my list, I have doors and normal nodes; Thinking about how to actually save them, in a form of a string or each one as class instance into the list. I just need to detect wether the current index is a door or a regular node.
What would you say?
Thank you!
I would use neither if speed is a concern. I would use an enum
Enum is even worse
Any string comparing or rendering is relatively slow, if you have to choose between these two, I'd probably go with typeof.
Are they not both objects either way making the point moot in terms of how C# and Unity work with objects. As with Enum, it is an object and passes things by value.
The fastest method is to pass something by reference.
C# has a built in type known as a delegate that can handle this type of communication between objects as opposed to using value swapping.
I warn you Delegates arent a small section of C# but they are well handled and fairly easy to implement.
I made this a while ago based from a tutorial I found on the net somewhere. Its a simple delegate system that shows how the button pushes are used to fire events and that they use a delegate to handle how the events are fired/dealt with.
[link text][1]
If you download the above link, unzip it, import into unity.... I think there is a Scene with it, else just attach the delegate menu script(C# file), found after importing package, to an empty game object in your scene, press play. it should pop up okay.
Anyway, hope that helps you to start your journey with delegates, if you havent already come to the conclusion that this way is faster than all that have been mentioned here so far<-(05.01.2014)
Take care & Happy New year Gruffy [1]: /storage/temp/20260-delegates.zip
Your answer
![](https://koobas.hobune.stream/wayback/20220613125625im_/https://answers.unity.com/themes/thub/images/avi.jpg)