Есть код, очень - очень медленный, нужно ускорить ну пару хотя-бы раз в 16. конструктор и деструктор не учитываются в бенчмарке Какие есть идеи?
#include <stdlib.h>
struct BallsManager
{
BallsManager(size_t count);
~BallsManager();
void process();
float * x;
float * y;
float * dir_x;
float * dir_y;
size_t m_count;
float * m_buffer;
};
BallsManager::BallsManager(size_t count):
m_count(count)
{
m_buffer = new float[count * 4] ;
x = m_buffer;
dir_x = (x + count);
y = (dir_x + count);
dir_y = (y + m_count);
for (size_t i = 0; i < m_count; ++i)
x[i] = (i % 200) + 5.0f;
for (size_t i = 0; i < m_count; ++i)
dir_x[i] = 1.0f;
for (size_t i = 0; i < m_count; ++i)
y[i] = (i % 200) + 5.0f;
for (size_t i = 0; i < m_count; ++i)
dir_y[i] = 1.0f;
}
BallsManager::~BallsManager()
{
delete[] m_buffer;
}
void BallsManager::process()
{
static const float min_x = 0.0f;
static const float max_x = 640.0f;
static const float min_y = 0.0f;
static const float max_y = 480.0f;
for (size_t i = 0; i < m_count; ++i)
{
float curr_x = x[i];
float curr_y = y[i];
float curr_dir_x = dir_x[i];
float curr_dir_y = dir_y[i];
if (curr_x <= min_x || curr_x >= max_x)
{
curr_dir_x = -curr_dir_x;
dir_x[i] = curr_dir_x;
}
x[i] = curr_x + curr_dir_x;
if (curr_y <= min_y || curr_y >= max_y)
{
curr_dir_y = -curr_dir_y;
dir_y[i] = curr_dir_y;
}
y[i] = curr_y + curr_dir_y;
}
}
int main()
{
BallsManager bm(15000);
//-- need to speedup from here --
for (int iterations = 0; iterations < 1000000; ++iterations)
bm.process();
//-- to here --
return 0;
}