2014-01-16 10:19:52 +08:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "PerformanceContainerTest.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
// Enable profiles for this file
|
|
|
|
#undef CC_PROFILER_DISPLAY_TIMERS
|
|
|
|
#define CC_PROFILER_DISPLAY_TIMERS() Profiler::getInstance()->displayTimers()
|
|
|
|
#undef CC_PROFILER_PURGE_ALL
|
|
|
|
#define CC_PROFILER_PURGE_ALL() Profiler::getInstance()->releaseAllTimers()
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START
|
|
|
|
#define CC_PROFILER_START(__name__) ProfilingBeginTimingBlock(__name__)
|
|
|
|
#undef CC_PROFILER_STOP
|
|
|
|
#define CC_PROFILER_STOP(__name__) ProfilingEndTimingBlock(__name__)
|
|
|
|
#undef CC_PROFILER_RESET
|
|
|
|
#define CC_PROFILER_RESET(__name__) ProfilingResetTimingBlock(__name__)
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START_CATEGORY
|
|
|
|
#define CC_PROFILER_START_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingBeginTimingBlock(__name__); } while(0)
|
|
|
|
#undef CC_PROFILER_STOP_CATEGORY
|
|
|
|
#define CC_PROFILER_STOP_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingEndTimingBlock(__name__); } while(0)
|
|
|
|
#undef CC_PROFILER_RESET_CATEGORY
|
|
|
|
#define CC_PROFILER_RESET_CATEGORY(__cat__, __name__) do{ if(__cat__) ProfilingResetTimingBlock(__name__); } while(0)
|
|
|
|
|
|
|
|
#undef CC_PROFILER_START_INSTANCE
|
|
|
|
#define CC_PROFILER_START_INSTANCE(__id__, __name__) do{ ProfilingBeginTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
#undef CC_PROFILER_STOP_INSTANCE
|
|
|
|
#define CC_PROFILER_STOP_INSTANCE(__id__, __name__) do{ ProfilingEndTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
#undef CC_PROFILER_RESET_INSTANCE
|
|
|
|
#define CC_PROFILER_RESET_INSTANCE(__id__, __name__) do{ ProfilingResetTimingBlock( String::createWithFormat("%08X - %s", __id__, __name__)->getCString() ); } while(0)
|
|
|
|
|
|
|
|
static std::function<PerformanceContainerScene*()> createFunctions[] =
|
|
|
|
{
|
|
|
|
CL(TemplateVectorPerfTest),
|
2014-01-17 13:57:58 +08:00
|
|
|
CL(ArrayPerfTest),
|
2014-01-20 11:12:29 +08:00
|
|
|
CL(TemplateMapStringKeyPerfTest),
|
|
|
|
CL(DictionaryStringKeyPerfTest),
|
|
|
|
CL(TemplateMapIntKeyPerfTest),
|
|
|
|
CL(DictionaryIntKeyPerfTest)
|
2014-01-16 10:19:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0]))
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kTagInfoLayer = 1,
|
|
|
|
|
|
|
|
kTagBase = 20000,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kMaxNodes = 15000,
|
|
|
|
kNodesIncrease = 500,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int g_curCase = 0;
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ContainerBasicLayer
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
ContainerBasicLayer::ContainerBasicLayer(bool bControlMenuVisible, int nMaxCases, int nCurCase)
|
|
|
|
: PerformBasicLayer(bControlMenuVisible, nMaxCases, nCurCase)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ContainerBasicLayer::showCurrentTest()
|
|
|
|
{
|
|
|
|
int nodes = ((PerformanceContainerScene*)getParent())->getQuantityOfNodes();
|
|
|
|
|
|
|
|
auto scene = createFunctions[_curCase]();
|
|
|
|
|
|
|
|
g_curCase = _curCase;
|
|
|
|
|
|
|
|
if (scene)
|
|
|
|
{
|
|
|
|
scene->initWithQuantityOfNodes(nodes);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// PerformanceContainerScene
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
void PerformanceContainerScene::initWithQuantityOfNodes(unsigned int nNodes)
|
|
|
|
{
|
|
|
|
_type = 0;
|
|
|
|
//srand(time());
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// Title
|
2014-04-09 23:31:05 +08:00
|
|
|
auto label = Label::createWithTTF(title().c_str(), "fonts/arial.ttf", 32);
|
2014-01-16 10:19:52 +08:00
|
|
|
addChild(label, 1, TAG_TITLE);
|
2014-02-10 11:42:15 +08:00
|
|
|
label->setPosition(Point(s.width/2, s.height-50));
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
// Subtitle
|
|
|
|
std::string strSubTitle = subtitle();
|
|
|
|
if(strSubTitle.length())
|
|
|
|
{
|
2014-04-09 23:31:05 +08:00
|
|
|
auto l = Label::createWithTTF(strSubTitle.c_str(), "fonts/Thonburi.ttf", 16);
|
2014-01-16 10:19:52 +08:00
|
|
|
addChild(l, 1, TAG_SUBTITLE);
|
|
|
|
l->setPosition(Point(s.width/2, s.height-80));
|
|
|
|
}
|
|
|
|
|
|
|
|
lastRenderedCount = 0;
|
|
|
|
currentQuantityOfNodes = 0;
|
|
|
|
quantityOfNodes = nNodes;
|
|
|
|
|
|
|
|
MenuItemFont::setFontSize(65);
|
2014-02-20 10:53:49 +08:00
|
|
|
auto decrease = MenuItemFont::create(" - ", [&](Ref *sender) {
|
2014-01-16 10:19:52 +08:00
|
|
|
quantityOfNodes -= kNodesIncrease;
|
|
|
|
if( quantityOfNodes < 0 )
|
|
|
|
quantityOfNodes = 0;
|
|
|
|
|
|
|
|
updateQuantityLabel();
|
|
|
|
updateQuantityOfNodes();
|
|
|
|
updateProfilerName();
|
|
|
|
CC_PROFILER_PURGE_ALL();
|
|
|
|
srand(0);
|
|
|
|
});
|
|
|
|
decrease->setColor(Color3B(0,200,20));
|
|
|
|
_decrease = decrease;
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto increase = MenuItemFont::create(" + ", [&](Ref *sender) {
|
2014-01-16 10:19:52 +08:00
|
|
|
quantityOfNodes += kNodesIncrease;
|
|
|
|
if( quantityOfNodes > kMaxNodes )
|
|
|
|
quantityOfNodes = kMaxNodes;
|
|
|
|
|
|
|
|
updateQuantityLabel();
|
|
|
|
updateQuantityOfNodes();
|
|
|
|
updateProfilerName();
|
|
|
|
CC_PROFILER_PURGE_ALL();
|
|
|
|
srand(0);
|
|
|
|
});
|
|
|
|
increase->setColor(Color3B(0,200,20));
|
|
|
|
_increase = increase;
|
|
|
|
|
|
|
|
auto menu = Menu::create(decrease, increase, NULL);
|
|
|
|
menu->alignItemsHorizontally();
|
|
|
|
menu->setPosition(Point(s.width/2, s.height/2+15));
|
|
|
|
addChild(menu, 1);
|
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto infoLabel = Label::createWithTTF("0 nodes", "fonts/Marker Felt.ttf", 30);
|
2014-01-16 10:19:52 +08:00
|
|
|
infoLabel->setColor(Color3B(0,200,20));
|
|
|
|
infoLabel->setPosition(Point(s.width/2, s.height/2-15));
|
|
|
|
addChild(infoLabel, 1, kTagInfoLayer);
|
|
|
|
|
|
|
|
auto menuLayer = new ContainerBasicLayer(true, MAX_LAYER, g_curCase);
|
|
|
|
addChild(menuLayer);
|
|
|
|
menuLayer->release();
|
|
|
|
|
|
|
|
printf("Size of Node: %lu\n", sizeof(Node));
|
|
|
|
|
|
|
|
int oldFontSize = MenuItemFont::getFontSize();
|
|
|
|
MenuItemFont::setFontSize(24);
|
|
|
|
|
|
|
|
Vector<cocos2d::MenuItem *> toggleItems;
|
|
|
|
|
|
|
|
generateTestFunctions();
|
|
|
|
|
|
|
|
CCASSERT(!_testFunctions.empty(), "Should not be empty after generate test functions");
|
|
|
|
|
|
|
|
|
|
|
|
for (const auto& f : _testFunctions)
|
|
|
|
{
|
|
|
|
toggleItems.pushBack(MenuItemFont::create(f.name));
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto toggle = MenuItemToggle::createWithCallback([this](Ref* sender){
|
2014-01-16 10:19:52 +08:00
|
|
|
auto toggle = static_cast<MenuItemToggle*>(sender);
|
|
|
|
this->_type = toggle->getSelectedIndex();
|
2014-03-26 23:33:58 +08:00
|
|
|
auto label = static_cast<Label*>(this->getChildByTag(TAG_SUBTITLE));
|
2014-01-16 10:19:52 +08:00
|
|
|
label->setString(StringUtils::format("Test '%s', See console", this->_testFunctions[this->_type].name));
|
|
|
|
this->updateProfilerName();
|
|
|
|
}, toggleItems);
|
|
|
|
|
|
|
|
toggle->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
|
|
|
|
toggle->setPosition(VisibleRect::left());
|
|
|
|
_toggle = toggle;
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto start = MenuItemFont::create("start", [this](Ref* sender){
|
2014-01-16 10:19:52 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto sched = director->getScheduler();
|
|
|
|
|
|
|
|
CC_PROFILER_PURGE_ALL();
|
2014-03-03 11:00:30 +08:00
|
|
|
sched->schedule(schedule_selector(PerformanceContainerScene::dumpProfilerInfo), this, 2, false);
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
this->unscheduleUpdate();
|
|
|
|
this->scheduleUpdate();
|
|
|
|
this->_startItem->setEnabled(false);
|
|
|
|
this->_stopItem->setEnabled(true);
|
|
|
|
this->_toggle->setEnabled(false);
|
|
|
|
this->_increase->setEnabled(false);
|
|
|
|
this->_decrease->setEnabled(false);
|
|
|
|
});
|
|
|
|
start->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
|
|
|
|
start->setPosition(VisibleRect::right() + Point(0, 40));
|
|
|
|
_startItem = start;
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
auto stop = MenuItemFont::create("stop", [this](Ref* sender){
|
2014-01-16 10:19:52 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto sched = director->getScheduler();
|
|
|
|
|
2014-03-03 11:00:30 +08:00
|
|
|
sched->unschedule(schedule_selector(PerformanceContainerScene::dumpProfilerInfo), this);
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
this->unscheduleUpdate();
|
|
|
|
this->_startItem->setEnabled(true);
|
|
|
|
this->_stopItem->setEnabled(false);
|
|
|
|
this->_toggle->setEnabled(true);
|
|
|
|
this->_increase->setEnabled(true);
|
|
|
|
this->_decrease->setEnabled(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
stop->setEnabled(false);
|
|
|
|
stop->setAnchorPoint(Point::ANCHOR_MIDDLE_RIGHT);
|
|
|
|
stop->setPosition(VisibleRect::right() + Point(0, -40));
|
|
|
|
_stopItem = stop;
|
|
|
|
|
|
|
|
auto menu2 = Menu::create(toggle, start, stop, NULL);
|
|
|
|
menu2->setPosition(Point::ZERO);
|
|
|
|
addChild(menu2);
|
|
|
|
|
|
|
|
MenuItemFont::setFontSize(oldFontSize);
|
|
|
|
|
|
|
|
updateQuantityLabel();
|
|
|
|
updateQuantityOfNodes();
|
|
|
|
updateProfilerName();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PerformanceContainerScene::title() const
|
|
|
|
{
|
|
|
|
return "No title";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string PerformanceContainerScene::subtitle() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerformanceContainerScene::updateQuantityLabel()
|
|
|
|
{
|
|
|
|
if( quantityOfNodes != lastRenderedCount )
|
|
|
|
{
|
2014-03-26 23:33:58 +08:00
|
|
|
auto infoLabel = static_cast<Label*>( getChildByTag(kTagInfoLayer) );
|
2014-01-16 10:19:52 +08:00
|
|
|
char str[20] = {0};
|
|
|
|
sprintf(str, "%u nodes", quantityOfNodes);
|
|
|
|
infoLabel->setString(str);
|
|
|
|
|
|
|
|
lastRenderedCount = quantityOfNodes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char * PerformanceContainerScene::profilerName()
|
|
|
|
{
|
|
|
|
return _profilerName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerformanceContainerScene::updateProfilerName()
|
|
|
|
{
|
|
|
|
snprintf(_profilerName, sizeof(_profilerName)-1, "%s(%d)", testName(), quantityOfNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerformanceContainerScene::dumpProfilerInfo(float dt)
|
|
|
|
{
|
|
|
|
CC_PROFILER_DISPLAY_TIMERS();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerformanceContainerScene::update(float dt)
|
|
|
|
{
|
|
|
|
_testFunctions[_type].func();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PerformanceContainerScene::updateQuantityOfNodes()
|
|
|
|
{
|
|
|
|
currentQuantityOfNodes = quantityOfNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* PerformanceContainerScene::testName()
|
|
|
|
{
|
|
|
|
return _testFunctions[_type].name;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TemplateVectorPerfTest
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TemplateVectorPerfTest::generateTestFunctions()
|
|
|
|
{
|
|
|
|
auto createVector = [this](){
|
|
|
|
Vector<Node*> ret;
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret.pushBack(node);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-01-17 10:29:21 +08:00
|
|
|
TestFunction testFunctions[] = {
|
2014-01-16 10:19:52 +08:00
|
|
|
{ "pushBack", [=](){
|
|
|
|
Vector<Node*> nodeVector;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.pushBack(Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "insert", [=](){
|
|
|
|
Vector<Node*> nodeVector;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.insert(0, Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "replace", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
2014-03-20 16:24:55 +08:00
|
|
|
srand((unsigned)time(nullptr));
|
2014-01-16 10:19:52 +08:00
|
|
|
ssize_t index = rand() % quantityOfNodes;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.replace(index, Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "getIndex", [=](){
|
|
|
|
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
Node* objToGet = nodeVector.at(quantityOfNodes/3);
|
|
|
|
ssize_t index = 0;
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
index = nodeVector.getIndex(objToGet);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
2014-01-16 14:41:31 +08:00
|
|
|
// Uses `index` to avoids `getIndex` invoking was optimized in release mode
|
|
|
|
if (index == quantityOfNodes/3)
|
|
|
|
{
|
|
|
|
nodeVector.clear();
|
|
|
|
}
|
2014-01-16 10:19:52 +08:00
|
|
|
} } ,
|
|
|
|
{ "find", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
Node* objToGet = nodeVector.at(quantityOfNodes/3);
|
2014-01-16 14:41:31 +08:00
|
|
|
Vector<Node*>::iterator iter;
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
2014-01-16 14:41:31 +08:00
|
|
|
iter = nodeVector.find(objToGet);
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
2014-01-16 14:41:31 +08:00
|
|
|
|
|
|
|
// Uses `iter` to avoids `find` invoking was optimized in release mode
|
|
|
|
if (*iter == objToGet)
|
|
|
|
{
|
|
|
|
nodeVector.clear();
|
|
|
|
}
|
|
|
|
|
2014-01-16 10:19:52 +08:00
|
|
|
} } ,
|
|
|
|
{ "at", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
2014-01-16 14:41:31 +08:00
|
|
|
Node* objToGet = nullptr;
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
2014-01-16 14:41:31 +08:00
|
|
|
objToGet = nodeVector.at(quantityOfNodes/3);
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
2014-01-16 14:41:31 +08:00
|
|
|
|
|
|
|
// Uses `objToGet` to avoids `at` invoking was optimized in release mode
|
|
|
|
if (nodeVector.getIndex(objToGet) == quantityOfNodes/3)
|
|
|
|
{
|
|
|
|
nodeVector.clear();
|
|
|
|
}
|
2014-01-16 10:19:52 +08:00
|
|
|
} } ,
|
|
|
|
{ "contains", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
Node* objToGet = nodeVector.at(quantityOfNodes/3);
|
|
|
|
|
2014-01-16 14:41:31 +08:00
|
|
|
bool ret = false;
|
|
|
|
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
2014-01-16 14:41:31 +08:00
|
|
|
ret = nodeVector.contains(objToGet);
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
2014-01-16 14:41:31 +08:00
|
|
|
|
|
|
|
// Uses `ret` to avoids `contains` invoking was optimized in release mode
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
nodeVector.clear();
|
|
|
|
}
|
2014-01-16 10:19:52 +08:00
|
|
|
} } ,
|
|
|
|
{ "eraseObject", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i] = nodeVector.at(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.eraseObject(nodes[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector.empty(), "nodeVector was not empty.");
|
|
|
|
|
|
|
|
free(nodes);
|
|
|
|
} } ,
|
|
|
|
{ "erase", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.erase(nodeVector.begin());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector.empty(), "nodeVector was not empty.");
|
|
|
|
|
|
|
|
} } ,
|
|
|
|
{ "clear", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.clear();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector.empty(), "nodeVector was not empty.");
|
|
|
|
} } ,
|
|
|
|
{ "swap by index", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
int swapIndex1 = quantityOfNodes / 3;
|
|
|
|
int swapIndex2 = quantityOfNodes / 3 * 2;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.swap(swapIndex1, swapIndex2);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "swap by object", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
Node* swapNode1 = nodeVector.at(quantityOfNodes / 3);
|
|
|
|
Node* swapNode2 = nodeVector.at(quantityOfNodes / 3 * 2);
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.swap(swapNode1, swapNode2);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "reverse", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector.reverse();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
2014-01-16 15:51:01 +08:00
|
|
|
|
|
|
|
{ "c++11 Range Loop", [=](){
|
|
|
|
Vector<Node*> nodeVector = createVector();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for (const auto& e : nodeVector)
|
|
|
|
{
|
|
|
|
e->setTag(111);
|
|
|
|
}
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
2014-01-16 10:19:52 +08:00
|
|
|
};
|
|
|
|
|
2014-01-17 10:29:21 +08:00
|
|
|
for (const auto& func : testFunctions)
|
2014-01-16 10:19:52 +08:00
|
|
|
{
|
2014-01-17 10:29:21 +08:00
|
|
|
_testFunctions.push_back(func);
|
2014-01-16 10:19:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TemplateVectorPerfTest::title() const
|
|
|
|
{
|
|
|
|
return "Vector<T> Perf test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TemplateVectorPerfTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Test 'pushBack', See console";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// ArrayPerfTest
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
std::string ArrayPerfTest::title() const
|
|
|
|
{
|
|
|
|
return "Array Perf test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ArrayPerfTest::subtitle() const
|
|
|
|
{
|
2014-01-16 14:41:31 +08:00
|
|
|
return "Test `addObject`, See console";
|
2014-01-16 10:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArrayPerfTest::generateTestFunctions()
|
|
|
|
{
|
|
|
|
auto createArray = [this](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* ret = Array::create();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret->addObject(node);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-01-17 10:29:21 +08:00
|
|
|
TestFunction testFunctions[] = {
|
2014-01-16 10:19:52 +08:00
|
|
|
{ "addObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = Array::create();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->addObject(Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "insertObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = Array::create();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->insertObject(Node::create(), 0);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "setObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
2014-03-20 16:24:55 +08:00
|
|
|
srand((unsigned)time(nullptr));
|
2014-01-16 10:19:52 +08:00
|
|
|
ssize_t index = rand() % quantityOfNodes;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->setObject(Node::create(), index);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "getIndexOfObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* objToGet = nodeVector->getObjectAtIndex(quantityOfNodes/3);
|
2014-01-16 14:41:31 +08:00
|
|
|
ssize_t index = 0;
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
2014-01-16 14:41:31 +08:00
|
|
|
index = nodeVector->getIndexOfObject(objToGet);
|
2014-01-16 10:19:52 +08:00
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
2014-01-16 14:41:31 +08:00
|
|
|
// Uses `index` to avoids `getIndex` invoking was optimized in release mode
|
|
|
|
if (index == quantityOfNodes/3)
|
|
|
|
{
|
|
|
|
nodeVector->removeAllObjects();
|
|
|
|
}
|
2014-01-16 10:19:52 +08:00
|
|
|
} } ,
|
|
|
|
{ "getObjectAtIndex", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->getObjectAtIndex(quantityOfNodes/3);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "containsObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* objToGet = nodeVector->getObjectAtIndex(quantityOfNodes/3);
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->containsObject(objToGet);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
{ "removeObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i] = static_cast<Node*>(nodeVector->getObjectAtIndex(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->removeObject(nodes[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
|
|
|
|
|
|
|
|
free(nodes);
|
|
|
|
} } ,
|
|
|
|
{ "removeObjectAtIndex", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->removeObjectAtIndex(0);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
|
|
|
|
|
|
|
|
} } ,
|
|
|
|
{ "removeAllObjects", [=](){
|
2014-03-20 16:24:55 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->removeAllObjects();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CCASSERT(nodeVector->count() == 0, "nodeVector was not empty.");
|
|
|
|
} } ,
|
|
|
|
{ "swap by index", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
int swapIndex1 = quantityOfNodes / 3;
|
|
|
|
int swapIndex2 = quantityOfNodes / 3 * 2;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->swap(swapIndex1, swapIndex2);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "swap by object", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* swapNode1 = nodeVector->getObjectAtIndex(quantityOfNodes / 3);
|
|
|
|
Ref* swapNode2 = nodeVector->getObjectAtIndex(quantityOfNodes / 3 * 2);
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->exchangeObject(swapNode1, swapNode2);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "reverseObjects", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-01-16 10:19:52 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodeVector->reverseObjects();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
2014-01-16 15:51:01 +08:00
|
|
|
|
|
|
|
{ "CCARRAY_FOREACH", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Array* nodeVector = createArray();
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-16 15:51:01 +08:00
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
|
|
|
|
CCARRAY_FOREACH(nodeVector, obj)
|
|
|
|
{
|
|
|
|
static_cast<Node*>(obj)->setTag(111);
|
|
|
|
}
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
2014-01-16 10:19:52 +08:00
|
|
|
};
|
|
|
|
|
2014-01-17 10:29:21 +08:00
|
|
|
for (const auto& func : testFunctions)
|
2014-01-16 10:19:52 +08:00
|
|
|
{
|
2014-01-17 10:29:21 +08:00
|
|
|
_testFunctions.push_back(func);
|
2014-01-16 10:19:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 13:57:58 +08:00
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2014-01-20 11:12:29 +08:00
|
|
|
// TemplateMapStringKeyPerfTest
|
2014-01-17 13:57:58 +08:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
void TemplateMapStringKeyPerfTest::generateTestFunctions()
|
2014-01-17 13:57:58 +08:00
|
|
|
{
|
|
|
|
auto createMap = [this](){
|
|
|
|
Map<std::string, Node*> ret;
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret.insert(StringUtils::format("key_%d", i), node);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
TestFunction testFunctions[] = {
|
2014-01-17 13:57:58 +08:00
|
|
|
{ "insert", [=](){
|
|
|
|
Map<std::string, Node*> map;
|
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
map.insert(keys[i], Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "at", [=](){
|
|
|
|
Map<std::string, Node*> map = createMap();
|
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodes[i] = map.at(keys[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i]->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
{ "erase", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
map.erase(keys[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "clear", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
map.clear();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "size", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
ssize_t size = 0;
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
size = map.size();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "keys(all)", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = map.keys();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
std::string allKeysString;
|
|
|
|
for (const auto& key : keys)
|
|
|
|
{
|
|
|
|
allKeysString += "_" + key;
|
|
|
|
}
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "keys(object)", [=](){
|
|
|
|
Map<std::string, Node*> map;
|
|
|
|
|
|
|
|
Node** nodes = (Node**) malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
Node* sameNode = Node::create();
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
if (quantityOfNodes % 100 == 0)
|
|
|
|
{
|
|
|
|
map.insert(StringUtils::format("key_%d", i), sameNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
map.insert(StringUtils::format("key_%d", i), node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = map.keys(sameNode);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
std::string allKeysString;
|
|
|
|
for (const auto& key : keys)
|
|
|
|
{
|
|
|
|
allKeysString += "_" + key;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "c++11 range loop", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
|
|
|
|
for (const auto& e : map)
|
|
|
|
{
|
|
|
|
e.second->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
2014-01-17 13:57:58 +08:00
|
|
|
};
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
for (const auto& func : testFunctions)
|
2014-01-17 13:57:58 +08:00
|
|
|
{
|
2014-01-20 11:12:29 +08:00
|
|
|
_testFunctions.push_back(func);
|
2014-01-17 13:57:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
std::string TemplateMapStringKeyPerfTest::title() const
|
2014-01-17 13:57:58 +08:00
|
|
|
{
|
2014-01-20 11:12:29 +08:00
|
|
|
return "Map<T> String Key Perf test";
|
2014-01-17 13:57:58 +08:00
|
|
|
}
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
std::string TemplateMapStringKeyPerfTest::subtitle() const
|
2014-01-17 13:57:58 +08:00
|
|
|
{
|
|
|
|
return "Test 'insert', See console";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
2014-01-20 11:12:29 +08:00
|
|
|
// DictionaryStringKeyPerfTest
|
2014-01-17 13:57:58 +08:00
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
void DictionaryStringKeyPerfTest::generateTestFunctions()
|
2014-01-17 13:57:58 +08:00
|
|
|
{
|
|
|
|
auto createDict = [this](){
|
2014-03-20 16:24:55 +08:00
|
|
|
__Dictionary* ret = __Dictionary::create();
|
2014-01-17 13:57:58 +08:00
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret->setObject(node, StringUtils::format("key_%d", i));
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestFunction testFunctions[] = {
|
|
|
|
{ "setObject", [=](){
|
2014-03-20 16:24:55 +08:00
|
|
|
__Dictionary* dict = __Dictionary::create();
|
2014-01-17 13:57:58 +08:00
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
dict->setObject(Node::create(), keys[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "objectForKey", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodes[i] = static_cast<Node*>(dict->objectForKey(keys[i]));
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i]->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
{ "removeObjectForKey", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
std::string* keys = new std::string[quantityOfNodes];
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
keys[i] = StringUtils::format("key_%d", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
dict->removeObjectForKey(keys[i]);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_DELETE_ARRAY(keys);
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "removeAllObjects", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
dict->removeAllObjects();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "count", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
ssize_t size = 0;
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
size = dict->count();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "allKeys", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = dict->allKeys();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
std::string allKeysString;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-20 11:12:29 +08:00
|
|
|
CCARRAY_FOREACH(keys, obj)
|
|
|
|
{
|
2014-03-05 15:04:30 +08:00
|
|
|
auto key = static_cast<__String*>(obj);
|
2014-01-20 11:12:29 +08:00
|
|
|
allKeysString += (std::string("_") + key->getCString());
|
|
|
|
}
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "allKeysForObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Dictionary* dict = Dictionary::create();
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
Node** nodes = (Node**) malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
Node* sameNode = Node::create();
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
if (quantityOfNodes % 100 == 0)
|
|
|
|
{
|
|
|
|
dict->setObject(sameNode, StringUtils::format("key_%d", i));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
dict->setObject(node, StringUtils::format("key_%d", i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = dict->allKeysForObject(sameNode);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
std::string allKeysString;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-20 11:12:29 +08:00
|
|
|
CCARRAY_FOREACH(keys, obj)
|
|
|
|
{
|
2014-03-05 15:04:30 +08:00
|
|
|
auto key = static_cast<__String*>(obj);
|
2014-01-20 11:12:29 +08:00
|
|
|
allKeysString += (std::string("_") + key->getCString());
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "CCDICT_FOREACH", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
|
|
|
|
DictElement* e = nullptr;
|
|
|
|
CCDICT_FOREACH(dict, e)
|
|
|
|
{
|
|
|
|
static_cast<Node*>(e->getObject())->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
2014-01-17 13:57:58 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& func : testFunctions)
|
|
|
|
{
|
|
|
|
_testFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-20 11:12:29 +08:00
|
|
|
std::string DictionaryStringKeyPerfTest::title() const
|
|
|
|
{
|
|
|
|
return "Dictionary String Key Perf test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DictionaryStringKeyPerfTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Test `setObject`, See console";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// TemplateMapIntKeyPerfTest
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TemplateMapIntKeyPerfTest::generateTestFunctions()
|
|
|
|
{
|
|
|
|
auto createMap = [this](){
|
|
|
|
Map<int, Node*> ret;
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret.insert(100+i, node);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestFunction testFunctions[] = {
|
|
|
|
{ "insert", [=](){
|
|
|
|
Map<int, Node*> map;
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
map.insert(100 + i, Node::create());
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "at", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodes[i] = map.at(100 + i);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i]->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "erase", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
map.erase(100 + i);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "clear", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
map.clear();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "size", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
ssize_t size = 0;
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
size = map.size();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "keys(all)", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = map.keys();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
int allKeysInt = 0;
|
|
|
|
for (const auto& key : keys)
|
|
|
|
{
|
|
|
|
allKeysInt += key;
|
|
|
|
}
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "keys(object)", [=](){
|
|
|
|
Map<int, Node*> map;
|
|
|
|
|
|
|
|
Node** nodes = (Node**) malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
Node* sameNode = Node::create();
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
if (quantityOfNodes % 100 == 0)
|
|
|
|
{
|
|
|
|
map.insert(100 + i, sameNode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
map.insert(100 + i, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = map.keys(sameNode);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
int allKeysInt = 0;
|
|
|
|
for (const auto& key : keys)
|
|
|
|
{
|
|
|
|
allKeysInt += key;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "c++11 range loop", [=](){
|
|
|
|
auto map = createMap();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
|
|
|
|
for (const auto& e : map)
|
|
|
|
{
|
|
|
|
e.second->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& func : testFunctions)
|
|
|
|
{
|
|
|
|
_testFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TemplateMapIntKeyPerfTest::title() const
|
|
|
|
{
|
|
|
|
return "Map<T> Integer Key Perf test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TemplateMapIntKeyPerfTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Test 'insert', See console";
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// DictionaryIntKeyPerfTest
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void DictionaryIntKeyPerfTest::generateTestFunctions()
|
|
|
|
{
|
|
|
|
auto createDict = [this](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Dictionary* ret = Dictionary::create();
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
ret->setObject(node, 100 + i);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
|
|
|
TestFunction testFunctions[] = {
|
|
|
|
{ "setObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Dictionary* dict = Dictionary::create();
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
dict->setObject(Node::create(), 100 + i);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "objectForKey", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
nodes[i] = static_cast<Node*>(dict->objectForKey(100 + i));
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
for (int i = 0; i < quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
nodes[i]->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "removeObjectForKey", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
Node** nodes = (Node**)malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
dict->removeObjectForKey(100 + i);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "removeAllObjects", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
dict->removeAllObjects();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "count", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
unsigned int size = 0;
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
size = dict->count();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "allKeys", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = dict->allKeys();
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
int allKeysInt = 0;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-20 11:12:29 +08:00
|
|
|
CCARRAY_FOREACH(keys, obj)
|
|
|
|
{
|
2014-03-05 15:04:30 +08:00
|
|
|
auto key = static_cast<__Integer*>(obj);
|
2014-01-20 11:12:29 +08:00
|
|
|
allKeysInt += key->getValue();
|
|
|
|
}
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "allKeysForObject", [=](){
|
2014-03-05 15:04:30 +08:00
|
|
|
__Dictionary* dict = Dictionary::create();
|
2014-01-20 11:12:29 +08:00
|
|
|
|
|
|
|
Node** nodes = (Node**) malloc(sizeof(Node*) * quantityOfNodes);
|
|
|
|
Node* sameNode = Node::create();
|
|
|
|
|
|
|
|
for( int i=0; i<quantityOfNodes; ++i)
|
|
|
|
{
|
|
|
|
if (quantityOfNodes % 100 == 0)
|
|
|
|
{
|
|
|
|
dict->setObject(sameNode, 100 + i);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto node = Node::create();
|
|
|
|
node->setTag(i);
|
|
|
|
dict->setObject(node, 100 + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
auto keys = dict->allKeysForObject(sameNode);
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
|
|
|
|
int allKeysInt = 0;
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* obj;
|
2014-01-20 11:12:29 +08:00
|
|
|
CCARRAY_FOREACH(keys, obj)
|
|
|
|
{
|
2014-03-05 15:04:30 +08:00
|
|
|
auto key = static_cast<__Integer*>(obj);
|
2014-01-20 11:12:29 +08:00
|
|
|
allKeysInt += key->getValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_FREE(nodes);
|
|
|
|
} } ,
|
|
|
|
|
|
|
|
{ "CCDICT_FOREACH", [=](){
|
|
|
|
auto dict = createDict();
|
|
|
|
|
|
|
|
CC_PROFILER_START(this->profilerName());
|
|
|
|
|
|
|
|
DictElement* e = nullptr;
|
|
|
|
CCDICT_FOREACH(dict, e)
|
|
|
|
{
|
|
|
|
static_cast<Node*>(e->getObject())->setTag(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_PROFILER_STOP(this->profilerName());
|
|
|
|
} } ,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto& func : testFunctions)
|
|
|
|
{
|
|
|
|
_testFunctions.push_back(func);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DictionaryIntKeyPerfTest::title() const
|
|
|
|
{
|
|
|
|
return "Dictionary Integer Key Perf test";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string DictionaryIntKeyPerfTest::subtitle() const
|
|
|
|
{
|
|
|
|
return "Test `setObject`, See console";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-16 10:19:52 +08:00
|
|
|
///----------------------------------------
|
|
|
|
void runContainerPerformanceTest()
|
|
|
|
{
|
|
|
|
auto scene = createFunctions[g_curCase]();
|
|
|
|
scene->initWithQuantityOfNodes(kNodesIncrease);
|
|
|
|
|
|
|
|
Director::getInstance()->replaceScene(scene);
|
|
|
|
}
|