To my surprise, once I made the program wait two seconds before starting each test, and then benchmark 10 cycles instead of one, the difference became noticeable.
I obtained between 1 to 2 seconds better performance in terms of latency with bitwise operators.
Code: Select all
#include <iostream>
#include <chrono>
#include <vector>
#include <string>
#include <thread>
// Define object states using bitwise operators
const int STATE_ACTIVE = 1 << 0; // 0001
const int STATE_INVISIBLE = 1 << 1; // 0010
const int PLAYER_COLLISION = 1 << 2; // 0100
const int ENEMY_COLLISION = 1 << 3; // 1000
// Conventional structure
struct ObjectState {
bool active;
bool invisible;
bool playerCollision;
bool enemyCollision;
};
// Function to measure execution time and memory usage
template <typename Func>
void measurePerformance(Func func, const std::string& name, int cycles) {
// Prepare the CPU
std::this_thread::sleep_for(std::chrono::seconds(2));
auto start = std::chrono::high_resolution_clock::now();
for (int i = 0; i < cycles; ++i) {
func();
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end - start;
std::cout << "Execution time of " << name << " for " << cycles << " cycles: " << duration.count() << " seconds" << std::endl;
}
// Function using bitwise operators
void bitwiseOperators() {
std::vector<int> objects(1000000, STATE_ACTIVE | STATE_INVISIBLE | ENEMY_COLLISION);
for (int state : objects) {
if (state & STATE_ACTIVE) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
int result = state & (1 << i);
}
}
if (state & STATE_INVISIBLE) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
int result = state & (1 << i);
}
}
if (state & PLAYER_COLLISION) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
int result = state & (1 << i);
}
}
if (state & ENEMY_COLLISION) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
int result = state & (1 << i);
}
}
}
}
// Function using conventional methods
void conventionalMethods() {
std::vector<ObjectState> objects(1000000, { true, true, false, true });
for (const ObjectState& state : objects) {
if (state.active) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
bool result = state.active && (i % 2 == 0);
}
}
if (state.invisible) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
bool result = state.invisible && (i % 2 == 0);
}
}
if (state.playerCollision) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
bool result = state.playerCollision && (i % 2 == 0);
}
}
if (state.enemyCollision) {
// Perform more complex operations
for (int i = 0; i < 10; ++i) {
// Simulation of complex operations
bool result = state.enemyCollision && (i % 2 == 0);
}
}
}
}
int main() {
int cycles = 10;
// Measure the performance of bitwise operators
measurePerformance(bitwiseOperators, "bitwise operators", cycles);
// Measure the performance of conventional methods
measurePerformance(conventionalMethods, "conventional methods", cycles);
// Measure memory usage
std::cout << "Size of an object with bitwise operators: " << sizeof(int) << " bytes" << std::endl;
std::cout << "Size of an object with conventional methods: " << sizeof(ObjectState) << " bytes" << std::endl;
system("pause");
return 0;
}