-1

I've been making a chess engine and I'm writing evaluation function and this is some part:

List<Double> eval;

switch (name) {
    case "PAWN":
        eval = new ArrayList<>(pawnEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (PAWN + eval.get(count)) * color;
        break;
    case "KNIGHT":
        eval = new ArrayList<>(knightEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (KNIGHT + eval.get(count)) * color;
        break;
    case "BISHOP":
        eval = new ArrayList<>(bishopEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (BISHOP + eval.get(count)) * color;
        break;
    case "ROOK":
        eval = new ArrayList<>(rookEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (ROOK + eval.get(count)) * color;
        break;
    case "QUEEN":
        eval = new ArrayList<>(queenEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (QUEEN + eval.get(count)) * color;
        break;
    case "KING":
        eval = new ArrayList<>(kingEval);
        if (color == -1)
            Collections.reverse(eval);


        evaluation += (KING + eval.get(count)) * color;
        break;
}

Creating another function and putting that code in it will cost less time than putting it directly in evaluation function so I tried putting that switch statement into a pieceEval method so it will just look like this:

evaluation += pieceEval(...) * color;
Before:
2876 total time (ms)
231786 nodes

After:
2184 total time (ms)
231786 nodes

My main question is: Does it really speed up code when extracting lines of codes into a method instead fitting it in one?

James Urian
  • 123
  • 3
  • 7
  • 3
    "Placing that in the evaluation function will cost more time than putting it directly in evaluation function" - what does this even mean? Sounds like you are saying the same thing. – hfontanez Feb 22 '21 at 12:58
  • 1
    Typical java beginners mistake: Java performance does rarely come out of clever java source code. It comes out of "normally looking" source code, that then gets optimized heavily at RUNTIME by the just in time compiler. In other words: you strive to write simple, easy to read and maintain code. – GhostCat Feb 22 '21 at 13:02
  • 1
    Especially for a complex thing such as a Chess engine: assume that being able to understand what your code is doing is absolute key. FIRST get your chess engine to work functionally (of course: avoid stupid performance killers), then when it works, and you are unhappy about performance, then MEASURE where your code spents it time, and start fixing that. – GhostCat Feb 22 '21 at 13:03
  • 1
    And of course, the other disclaimer: measuring java performance is *hard*: https://stackoverflow.com/questions/504103/how-do-i-write-a-correct-micro-benchmark-in-java – GhostCat Feb 22 '21 at 13:06
  • 1
    Finally: alone switching over "strings" is a bad starter. Your very first step should be to define a reasonable *object model*, and the *type* of your chess pieces for sure should be based on an Enum, not raw strings! – GhostCat Feb 22 '21 at 13:07
  • "Premature optimization is the root of all evil." -- Knuth. – NomadMaker Feb 22 '21 at 13:49

1 Answers1

0

A function evaluation typically takes more execution time than an inline code. Functions are used to ensure you are not copy pasting the same logic across hundred places. Consider that if you have to make the change to your logic, will it be easier to modify at a single place (function) or at hundred places where you have copy pasted the code. From a design point of vioew, I will use a function

akshaya pandey
  • 997
  • 6
  • 16