Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML\Graphics.hpp>
- #include <iostream>
- #include <vector>
- using namespace std;
- const int testCount = 100;
- //base class
- class GameObject : public sf::Sprite
- {
- private:
- public:
- enum Type
- {
- actor,
- other
- } type;
- };
- //class we're casting
- class Actor : public GameObject
- {
- private:
- public:
- Actor()
- {
- type = actor;
- }
- sf::Vector2f velocity;
- };
- //creates objects
- void CreateObjects(vector<GameObject*>& objects, const int max_objects)
- {
- for (int i = 0; i < max_objects; i++)
- {
- objects.push_back(new GameObject);
- }
- }
- //checks every object if it's equal to actor
- void StaticCast(vector<GameObject*>& objects)
- {
- for (auto it = objects.begin(); it != objects.end(); it++)
- {
- if ((*it)->type == GameObject::actor)
- {
- Actor* actor = static_cast<Actor*>(*it);
- actor->setPosition(sf::Vector2f(50, 50));
- }
- }
- }
- void DynamicCast(vector<GameObject*>& objects)
- {
- for (auto it = objects.begin(); it != objects.end(); it++)
- {
- Actor* actor = dynamic_cast<Actor*>(*it);
- if (actor)
- {
- actor->setPosition(sf::Vector2f(50, 50));
- }
- }
- }
- void DeleteObjects(std::vector<GameObject*>& objects)
- {
- for (auto it = objects.begin(); it != objects.end(); it++)
- {
- delete *it;
- }
- objects.clear();
- }
- struct Times
- {
- sf::Time averageDynamic;
- sf::Time averageStatic;
- };
- Times AverageTimes(sf::Time dynamicTimes[testCount], sf::Time staticTimes[testCount])
- {
- sf::Time averageDynamic;
- sf::Time averageStatic;
- for (int i = 0; i < testCount; i++)
- {
- averageDynamic += dynamicTimes[i];
- averageStatic += staticTimes[i];
- }
- __int64 tCount = testCount;
- averageDynamic /= tCount;
- averageStatic /= tCount;
- Times times;
- times.averageDynamic = averageDynamic;
- times.averageStatic = averageStatic;
- return times;
- }
- void TestLoop(vector<GameObject*>& objects, const int max_objects)
- {
- sf::Time dynamicTimes[testCount];
- sf::Time staticTimes[testCount];
- sf::Clock clock;
- for (int i = 0; i < testCount; i++)
- {
- CreateObjects(objects, max_objects);
- clock.restart();
- DynamicCast(objects);
- dynamicTimes[i] = clock.getElapsedTime();
- DeleteObjects(objects);
- clock.restart();
- StaticCast(objects);
- staticTimes[i] = clock.getElapsedTime();
- DeleteObjects(objects);
- std::cout << "done: " << i << endl;
- }
- Times times;
- times = AverageTimes(dynamicTimes, staticTimes);
- system("cls");
- std::cout << "Average Dynamic: " << times.averageDynamic.asMicroseconds() << endl
- << "Average Static: " << times.averageStatic.asMicroseconds() << endl;
- }
- int main()
- {
- vector<GameObject*> objects;
- const int max_objects = 1000;
- TestLoop(objects, max_objects);
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement