【发布时间】:2016-10-07 10:16:08
【问题描述】:
我对 C++ 很陌生,只想测试 C++ 能多快完成以下工作:
只需用 100 个 Object-Point (x,y-Coordinate) 的 Objectc 创建一个向量,然后将其移动到另一个向量。重复此 k 次。 (在此代码中,它是 1000000 次 - int Iterator)。
好吧,因为我对 C++ 很陌生,你有没有更好的方法,或者我错过了什么?
我在 Windows 上运行。
#include "Main.h"
#include "Point.h"
#include <iostream>
#include <vector>
#include <chrono>
int main() {
auto start = std::chrono::high_resolution_clock::now();
int Constant = 10;
int Iterator = 1000000;
std::vector<Point>* tour = new std::vector<Point>();
std::vector<Point>* actions = new std::vector<Point>();
for (int k=0; k<Iterator; k++) {
for (int i=0; i<Constant; i++) {
for (int j=0; j<Constant; j++) {
Point *p = new Point((i * 10) + j,i + 1, j + 1);
actions->push_back(*p);
}
}
while(!actions->empty()) {
tour->push_back(actions->at(0));
actions->erase(actions->begin());
}
actions->clear();
tour->clear();
}
auto finish = std::chrono::high_resolution_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::nanoseconds>(finish-start).count() << std::endl;
}
【问题讨论】:
-
程序运行了吗?它做你想让它做的事吗?那么你只需要一个code review。
-
如果你想改进工作代码,你最好把这个问题发到SE Code Review。
-
作业的附加信息:创建一个包含 100 个对象的向量,其中 x,y-坐标 + id 并通过按递增索引顺序添加对象并将其移动到另一个向量并删除原点向量中的对象
-
你的问题是什么?
-
我怀疑 this technique 将表明 new Point 和两个 push_back 语句基本上负责所有时间。
标签: c++ windows performance vector timestamp