If you're going this way you may want to skip the array and just do:Vindekator said:You might try something like
switch(slct[a,b])
{
case "1_5"{some stuff}
case "1_6"{some stuff}
case "1_7"{some stuff}
case "2_5"{some stuff}
case "3_5"{some stuff}
}
where you have set up the array slct to have the values required (also include a default if necessary)
You can increment or set your indexes and get a testable string from the array.
switch (a+","+b)
{
case "1,5": bla bla
case "1,6": bla bla
case "1,7": bla bla
case "2,5": bla bla
case "3,5": bla bla
}
if ( a==1)
{
if (b==5)
stuff
else if (b==6)
stuff
else if (b==7)
stuff
}
else if (b==5)
{
if (a==2)
stuff
else if (a==3)
stuff
}
if (a==1)
//bla bla
else
//..
if (a==xx && b==zz)
else if ..
if (a==xx && b==zz)
else if ..
On the countrary, using a string will be a lot slower than if/else.noobie said:I really doubt using switch with string concat would much faster than regular a few if-else statements. btw it also has to build that look up table.
anyway event it were faster, does it really matter? how faster could it make it in such a simple case?? nanoseconds??
using System;
using System.Collections;
using System.Collections.Generic;
//------------------------------------------------------------------------------
public class Test
{
private static int[,] m_Integers = new int[,]
{
{ 1, 3 },
{ 3, 3 },
{ 3, 5 },
{ 5, 5 },
{ 5, 7 },
{ 7, 7 },
{ 7, 11 },
{ 11, 11 },
{ 11, 13 },
{ 13, 13 },
};
private static string[,] m_Strings = new string[,]
{
{ "thisisaridculouslylongstring_one", "thisisaridculouslylongstring_three" },
{ "thisisaridculouslylongstring_three", "thisisaridculouslylongstring_three" },
{ "thisisaridculouslylongstring_three", "thisisaridculouslylongstring_five" },
{ "thisisaridculouslylongstring_five", "thisisaridculouslylongstring_five" },
{ "thisisaridculouslylongstring_five", "thisisaridculouslylongstring_seven" },
{ "thisisaridculouslylongstring_seven", "thisisaridculouslylongstring_seven" },
{ "thisisaridculouslylongstring_seven", "thisisaridculouslylongstring_eleven" },
{ "thisisaridculouslylongstring_eleven", "thisisaridculouslylongstring_eleven" },
{ "thisisaridculouslylongstring_eleven", "thisisaridculouslylongstring_thirteen" },
{ "thisisaridculouslylongstring_thirteen", "thisisaridculouslylongstring_thirteen" },
};
//------------------------------------------------------------------------------
public static void Main( string[] args )
{
{
double start = DateTime.Now.Ticks / 10000000.0;
long count = 0;
for( long i=0; i<100000000; i++)
{
int index = (int)( i % 10L );
if( m_Integers[index,0]==m_Integers[index,1] ) count++;
}
double stop = DateTime.Now.Ticks / 10000000.0;
double elapsed = stop - start;
Console.WriteLine("INTEGERS");
Console.WriteLine(" count: "+count);
Console.WriteLine(" elapsed: "+elapsed);
}
{
double start = DateTime.Now.Ticks / 10000000.0;
long count = 0;
for( long i=0; i<100000000; i++)
{
int index = (int)( i % 10L );
if( m_Strings[index,0]==m_Strings[index,1] ) count++;
}
double stop = DateTime.Now.Ticks / 10000000.0;
double elapsed = stop - start;
Console.WriteLine("STRINGS");
Console.WriteLine(" count: "+count);
Console.WriteLine(" elapsed: "+elapsed);
}
}
}
//------------------------------------------------------------------------------
Small difference doesnt mean it uses references to compare. And why should it mean something like that?
Integers don't need to. They are a unique number. You can use a single assembler instruction to compare them per se. Strings don't have that property on any modern silicon I know of. So they are "internalized" in such a fashion that any two strings that are spelled the same (are "lexigraphically identical" is the technical term) have the same address in memory. You can then know that if the address is the same, they are the same string, and if not, not.integer comparisons dont use reference comparisons..
The common language runtime conserves string storage by maintaining a table, called the intern pool, that contains a single reference to each unique literal string declared or created programmatically in your program. Consequently, an instance of a literal string with a particular value only exists once in the system. For example, if you assign the same literal string to several variables, the runtime retrieves the same reference to the literal string from the intern pool and assigns it to each variable.