コンテキスト
QLinkedList を使って、私が書いたクラスを保存しています。
実際、このリストをたくさん繰り返す必要があります。
多くの場合、私が書いたプログラムは無限の計算を行います(あなたはまだそれを手動で止めることができます)。そして、各繰り返しの QLinkedList を調べる必要があります。
問題
The 問題 is not if I'm iterating to much over this list.
それは私のコードをプロファイリングしているので、1/4の時間はQLinkedList :: end()と QLinkedList :: begin()関数に費やされていることがわかります。
サンプルコード
私のコードは次のとおりです:
typedef QLinkedList ParticlesList; //Particle is a custom class
ParticlesList* parts =//assign a QLinkedList
for (ParticlesList::const_iterator itp = parts->begin(); itp != parts->end(); ++itp)
{
//make some calculus
}
Like I said, this code is called so often that it spends a lot of time on parts->begin() and parts->end().
質問
So, the 質問 is how can I reduce the time spent on the iteration of this list ?
可能な解決策
ここで私が考えたいくつかのソリューションは、私が最高を選択するか、私に別のものを提案するのを助けてください:)
- 古典的なC配列の使用://このミスで申し訳ありません
Particle** parts =//assing it something
for (int n = 0; n < LENGTH; n++)
{
//access by index
//make some calculus
}
これはすぐにすべきですか?
- Javaスタイルのイテレータを使用している可能性がありますか?
- 別のコンテナを使用している可能性がありますか?
- Asm?ちょうど冗談...または多分?
今後のお返事ありがとうございます!
PS:プロファイルを作成するタイミングについては、stackoverflowの投稿を読んでいますので、心配しないでください;)
編集:
リストが変更されました
私は最も重要なことを忘れてしまったと思って申し訳ありません、私はストップせずに関数全体を記述します:
typedef std::vector Neighbours;
typedef QLinkedList ParticlesList;
Neighbours neighbours = m_cell->getNeighbourhood();
Neighbours::const_iterator it;
for (it = neighbours.begin(); it != neighbours.end(); ++it)
{
ParticlesList* parts = (*it)->getParticles();
for (ParticlesList::const_iterator itp = parts->begin(); itp != parts->end(); ++itp)
{
double d = distanceTo(*itp);//computes sqrt(x^2 + y^2)
if(d>=0 && d<=m_maxForceRange)
{
particleIsClose(d, *itp);//just changes
}
}
}
|
私が完全であることを確認するために、このコード全体がループで呼び出されます^^
So yes リストが変更されました and it is in a inner loop. So there's no way to precompute the beginning and end of it.
さらに、リストは、1つずつ挿入することによって、大きな反復ごとに構築する必要があります(一番上のループを意味します)。
デバッグモード
Yes indeed I profiled in デバッグモード. And I think the remark was judicious because the code went 2x faster in Release. And the 問題 with lists disappeared.
あなたの答えに感謝し、このことを申し訳ありません^^