Performancequestion

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. ;)
HamsterofDeath said:

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))

i went for the abstract one.

next problem. i want to do this:

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?

I'd write the whole thing in a shader, and compare the bitfields in parallel.



Or give each bitfield it's own little AI to work out what's fastest.





:stuck_out_tongue:

i thought about using multihreading, but it isn't possible. i only perform the check once after every move, not 42 times for the whole field.

This expression

HamsterofDeath said:

longs[i]&&spielfeld==longs[i]



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'.

longs[i]&&spielfeld==longs[i]



should be

masks[i]&spielfeld==masks[i]



i forgot to run the benchmark on a 64 bit machine… should be a lot faster

office machine (p4, 2.8 or 3 ghz, not sure), 32 bit: 18.000.000 checks/second

my machine (core2duo, 3.42 ghz at the moment of testing): 45.9 million checks/second

my machine, 64 bit vm: 66.4 million checks/second.



noticable speedup. also, the core2duo rocks. more than twice as fast as a p4 at the same clock speed. not bad, intel.