Main Site Documentation

== with strings


#1

Hi all,

I wanted to ask a quick question in regards to testing strings.

In a desktop C# application, I am able to do the following and it works.

if(string1 == "TEST") 
{
     ....
}

But this testing is always false with the microframework and I have had to use .equals(“TEST”) to get it to work.

Is this correct?


#2

Works ok for me 4.2 project

            string test = "Hello";
            if(test == "hello")
            {
                Debug.Print("Dont think so");
            }
            if(test == "Hello")
            {
                Debug.Print("Clever boy");
            }

#3

Just tested with a Vanilla 4.1 and it works ok…
Is string1 getting changed somewhere else?..


#4

Do we have any benchs that proves a performance enhancement?


#5

@ andre.m - i assume that test was done on the full framework?


#6

Here’s is my bench results about == and .Equals

== in,276834, micro seconds
Equals in,480458, micro seconds

For the following code:

using System;
using Microsoft.SPOT;

using GHIElectronics.NETMF.Hardware;

namespace stringcomparison_benchmark
{
    public class Program
    {
        public static void Main()
        {
            bool result1 = false;
            bool result2 = false;
            string[] s = new string[1000];
            for(int i = 0;i < 1000;i++)
            {
                s[i] = "abcd";
            }
            while(true)
            {
                DateTime t1 = DateTime.Now;
                for(int i = 0;i < 1000;i++)
                {
                    if(s[i] == "abcd")
                        result1 = true;
                    else result1 = false;
                    if(s[i] == "efgh")
                        result2 = true;
                    else result2 = false;
                }

                DateTime t2 = DateTime.Now;
                Debug.Print("Result1 = " + result1);
                Debug.Print("Result2 = " + result2);
                Debug.Print(" == in," + (t2 - t1).Ticks / 10 + ", micro seconds");

                t1 = DateTime.Now;
                for(int i = 0;i < 1000;i++)
                {
                    if(s[i].Equals("abcd"))
                        result1 = true;
                    else result1 = false;
                    if(s[i].Equals("efgh"))
                        result2 = true;
                    else result2 = false;
                }
                t2 = DateTime.Now;
                Debug.Print("Result1 = " + result1);
                Debug.Print("Result2 = " + result2);
                Debug.Print(" Equals in," + (t2 - t1).Ticks / 10 + ", micro seconds");

            }
        }

    }
}

Does my bench is relevant?


#7

tehehehe


#8

Sorry Andre, I do not understand your point. Do you mean something like this ?

using System;
using Microsoft.SPOT;

using GHIElectronics.NETMF.Hardware;

namespace stringcomparison_benchmark
{
    public class Program
    {
        public static void Main()
        {
            bool result1 = false;
            bool result2 = false;
            string[] s = new string[1000];
            for(int i = 0;i < 1000;i++)
            {
                s[i] = "abcd";
            }
            while(true)
            {
                DateTime t1;
                DateTime t2;

                t1 = DateTime.Now;
                for(int i = 0;i < 1000;i++)
                {
                    if(s[i] == "abcd")
                        result1 = true;
                    else result1 = false;
                    if(s[i] == "efgh")
                        result2 = true;
                    else result2 = false;
                }

                t2 = DateTime.Now;
                Debug.Print("Result1 = " + result1);
                Debug.Print("Result2 = " + result2);
                Debug.Print(" == in," + (t2 - t1).Ticks / 10 + ", micro seconds");

                t1 = DateTime.Now;
                for(int i = 0;i < 1000;i++)
                {
                    if(s[i].Equals("abcd"))
                        result1 = true;
                    else result1 = false;
                    if(s[i].Equals("efgh"))
                        result2 = true;
                    else result2 = false;
                }
                t2 = DateTime.Now;
                Debug.Print("Result1 = " + result1);
                Debug.Print("Result2 = " + result2);
                Debug.Print(" Equals in," + (t2 - t1).Ticks / 10 + ", micro seconds");

            }
        }

    }
}

As you have already said, there’s no difference after modification:
== in,276817, micro seconds
Equals in,480472, micro seconds