История изменений
Исправление invy, (текущая версия) :
оптимизация 1:
#include <stdlib.h>
#include <vector>
struct Ball {
float x;
float y;
float dir_x;
float dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = (i % 200) + 5.0f;
balls[i].dir_x = 1.0f;
balls[i].y = (i % 200) + 5.0f;
balls[i].dir_y = 1.0f;
}
}
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 (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
#include <stdlib.h>
#include <vector>
struct Ball {
int x;
int y;
int dir_x;
int dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = ((i % 200) + 5) * 100;
balls[i].dir_x = 100;
balls[i].y = ((i % 200) + 5) * 100;
balls[i].dir_y = 100;
}
}
void BallsManager::process()
{
static const int min_x = 0;
static const int max_x = 64000;
static const int min_y = 0;
static const int max_y = 48000;
for (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
100 000 итераций
$ time opt2.exe
real 0m3.373s
user 0m0.015s
sys 0m0.046s
$ time opt1.exe
real 0m4.248s
user 0m0.031s
sys 0m0.015s
$ time orig.exe
real 0m8.663s
user 0m0.031s
sys 0m0.015s
Исправление invy, :
оптимизация 1:
#include <stdlib.h>
#include <vector>
struct Ball {
float x;
float y;
float dir_x;
float dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = (i % 200) + 5.0f;
balls[i].dir_x = 1.0f;
balls[i].y = (i % 200) + 5.0f;
balls[i].dir_y = 1.0f;
}
}
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 (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
#include <stdlib.h>
#include <vector>
struct Ball {
int x;
int y;
int dir_x;
int dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = ((i % 200) + 5.0) * 100;
balls[i].dir_x = 100;
balls[i].y = ((i % 200) + 5.0) * 100;
balls[i].dir_y = 100;
}
}
void BallsManager::process()
{
static const int min_x = 0;
static const int max_x = 64000;
static const int min_y = 0;
static const int max_y = 48000;
for (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
100 000 итераций
$ time opt2.exe
real 0m3.373s
user 0m0.015s
sys 0m0.046s
$ time opt1.exe
real 0m4.248s
user 0m0.031s
sys 0m0.015s
$ time orig.exe
real 0m8.663s
user 0m0.031s
sys 0m0.015s
Исходная версия invy, :
оптимизация 1:
#include <stdlib.h>
#include <vector>
struct Ball {
float x;
float y;
float dir_x;
float dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = (i % 200) + 5.0f;
balls[i].dir_x = 1.0f;
balls[i].y = (i % 200) + 5.0f;
balls[i].dir_y = 1.0f;
}
}
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 (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
#include <stdlib.h>
#include <vector>
struct Ball {
int x;
int y;
int dir_x;
int dir_y;
};
struct BallsManager
{
BallsManager(size_t count);
void process();
size_t m_count;
std::vector<Ball> balls;
};
BallsManager::BallsManager(size_t count):
m_count(count), balls(count)
{
for (size_t i = 0; i < m_count; ++i) {
balls[i].x = (i % 200) + 5.0f;
balls[i].dir_x = 1.0f;
balls[i].y = (i % 200) + 5.0f;
balls[i].dir_y = 1.0f;
}
}
void BallsManager::process()
{
static const int min_x = 0;
static const int max_x = 64000;
static const int min_y = 0;
static const int max_y = 48000;
for (Ball &ball : balls)
{
if (ball.x <= min_x || ball.x >= max_x)
ball.dir_x *= -1;
ball.x += ball.dir_x;
if (ball.y <= min_y || ball.y >= max_y)
ball.dir_y *= -1;
ball.y += ball.dir_y;
}
}
100 000 итераций
$ time opt2.exe
real 0m3.373s
user 0m0.015s
sys 0m0.046s
$ time opt1.exe
real 0m4.248s
user 0m0.031s
sys 0m0.015s
$ time orig.exe
real 0m8.663s
user 0m0.031s
sys 0m0.015s