for a little side project (a connect 4 ai), i have to decide wether to use an abstract method and 10 different implementations or to call 10 different methods in a switch like this:
switch (key)
{
case 0,1,2,5,6: return method1();
case 27,42: return method2();
}
which way is faster? the switch, or an abstract method?
I think the abstract one is faster, because there you set the pointer while you initilize your object. The SwitchCase has to be called everytime you want to call the method. And a second Problem with the SwitchCase, when you want to change your code, you have to be always carefully that the keys AND and the keyhandling is correct, in an abstract object you have the in the same classFile.
You can build also one global abstract class, and for method one and two you have only diferrent implementations, which you can assign the keys you want
And a second Problem with the SwitchCase, when you want to change your code, you have to be always carefully that the keys AND and the keyhandling is correct
as long as connect 4 doesn't change, that won't be a problem. ;)
And a second Problem with the SwitchCase, when you want to change your code, you have to be always carefully that the keys AND and the keyhandling is correct
as long as connect 4 doesn't change, that won't be a problem. ;)
(actual) LOL
But yes abstract is faster. switches aren't that fast at all afaik.
(edit: unless they're densly packed "cases", but I don't know if Java even optimizes for that (could be though))
for (int i = longs.length;--i>=0;)
{
if (longs[i]&&spielfeld==longs[i])
{
return true;
}
}
return false;
the bitmasks are already generated, and now i want to avoid the loop overhead. i was planning to use one base class with 11 fields (longs) and subclass it for all possible amounts of bitmasks. subclass 1 checks 3 masks, subclass 2 checks 4 masks and so on.
so basically, the question is:
should i use 13 fields, or should i use a long-array and access the masks with hard coded numbers? does it matter at all?
Does not make much sense. If longs is declared as long[] it gives a compile error. If longs is declared as boolean[] the second reference to longs[ i] can be replaced by 'true'.