I'm currently translating my chess engine from java to c++. When I completed some parts of it, I noticed that calling those functions took much more time than they 'should'. When I put the function to call in the same file the main function is in, it's much faster, tho (244.569ms vs 0.0002ms). Why's that?
Here's the code:
main.cpp
#include <iostream>
#include <chrono>
using namespace std;
#include "utilities.h"
int main() {
using std::chrono::steady_clock;
using std::chrono::duration_cast;
using std::chrono::duration;
using std::chrono::milliseconds;
auto t1 = steady_clock::now();
int end = 0;
for (int i = 0; i < 100000000; i++) {
unsigned long b = rotate180(0xFFFF000000000000);
}
auto t2 = steady_clock::now();
auto ms_int = duration_cast<milliseconds>(t2 - t1);
duration<double, std::milli> ms_double = t2 - t1;
std::cout << ms_int.count() << "ms\n";
std::cout << ms_double.count() << "ms\n";
}
utilities.cpp
#include <iostream>
using namespace std;
#include "utilities.h"
// from https://www.chessprogramming.org/Flipping_Mirroring_and_Rotating
unsigned long rotate180(unsigned long x) {
const unsigned long h1 = 0x5555555555555555;
const unsigned long h2 = 0x3333333333333333;
const unsigned long h4 = 0x0F0F0F0F0F0F0F0F;
const unsigned long v1 = 0x00FF00FF00FF00FF;
const unsigned long v2 = 0x0000FFFF0000FFFF;
x = ((x >> 1) & h1) | ((x & h1) << 1);
x = ((x >> 2) & h2) | ((x & h2) << 2);
x = ((x >> 4) & h4) | ((x & h4) << 4);
x = ((x >> 8) & v1) | ((x & v1) << 8);
x = ((x >> 16) & v2) | ((x & v2) << 16);
x = ( x >> 32) | ( x << 32);
return x;
}
utlities.h
#ifndef UTILITIES_H
#define UTILITIES_H
unsigned long rotate180(unsigned long x);
#endif
I'm aware that this example doesn't do much, but it's already appearing here, so I'll have to deal with performance loss, when I'll do some complex calculations.