gpt4 book ai didi

c++ - 改进我的代码

转载 作者:可可西里 更新时间:2023-11-01 11:11:49 25 4
gpt4 key购买 nike

<分区>

我是 C++ 的新手,只想测试 C++ 完成以下工作的速度:

只需创建一个具有 100 个 Objectc 的对象点(x,y 坐标)的 vector 并将其移动到另一个 vector 。重复这个 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;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com