C ++如何加快我的prog设计 [英] C++ How to speed up my prog design

查看:128
本文介绍了C ++如何加快我的prog设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个和弦系统..

i am trying to design a chord system..

问题是我的系统工作正常,如果大小是10-20行addPeer,removePeer等。 。

the problem is my system works fine if the size is like 10-20 lines of addPeer, removePeer etc..

但是当我用类似5000行命令文件测试它。

but when i test it with a like 5000 lines of command file.

很快,但随着程序加载越来越多的行,它开始变慢了。

the first few hundreds was pretty fast, but as the program load more and more lines, it start to get slow..

由于程序的要求是测试我的程序设计,我不能使用线程。

As the requirement of the program is to test my program design, i cannot use threading.

我听说指针是一个很好的方式来完成事情做得更快,但我如何使用指针为我的情况。

I heard pointer is a good way to get things done faster, but how do i use pointer for my case.

这是我的类头。

class chord 
{
public:
chord();
~chord();

struct fingerTable {
int index;
int key;
};

struct node {
int nodeid;
vector<fingerTable> fTable;
vector<string> data;
};

void addPeer(int);

vector<node> cNode;
vector<fingerTable> fTable;

/* SOME more functions ..*/
};

这是我的addPeer函数

This is my addPeer Function

void chord::addPeer(int id)
{
//id = node ID
int fIndex,nextNode;
node newNode;
vector<fingerTable> ft1;
vector<string> data1;
//increment indexCounter
//indexCounter++;

newNode.nodeid = id;
//insert a blank fingerTable first.
newNode.fTable = ft1;
//insert a blank data first.
newNode.data = data1;

//push back node to vector chord Index Node
cNode.push_back(newNode);
//indexCounter++;
//perform finger table computation

//sort it base on its NodeID
sort(cNode.begin(),cNode.end(),sortByNodeID);

for(int i=0;i<cNode.size();i++)
{
if(cNode[i].nodeid==id)
{
fIndex=i;
}
}//end for loop to loop finding index of node

if(fIndex!=cNode.size()-1)
{
//if not last element
nextNode=fIndex+1;
}
else
{
nextNode=0;
}

//now we get the message vector of the next node and do a datashift on it.
data1 = cNode[nextNode].data;
//clear its data away so we can empty it and re-arrange it.
cNode[nextNode].data.clear();
//performing data shift function
dataShift(data1,fIndex-1);

if(id!=0)
{
cout << "PEER " << id << " inserted."<< endl;
}

}//end addPeer

,这部分可以即兴为这个函数addPeer使整个程序执行线更快。因为它执行几百行时真的很慢。

My question is, which part can i improvise on for this function addPeer to make the whole program execute the lines faster. as it get really slow when execute a few hundreds line.

推荐答案

这是放慢,因为你是不断排序。你应该prob。使用如 std :: map 的排序结构。

This is slowing down because you are constantly sorting. You should prob. be using a sorted structure like std::map<int,node> .

这篇关于C ++如何加快我的prog设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆