缓慢的C ++代码执行 [英] Slow C++ code execution

查看:155
本文介绍了缓慢的C ++代码执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好任何人都可以说为什么这段代码执行〜30分钟,我玩了编译器设置,代码在2分钟后执行,我不记得我刚刚做了什么

Hello Can anyone say why this code took to execute ~30minutes, I have played with compiler settings and code was executed in 2 minutes, I cant remember what I just did

以下代码:

// sampleC.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <chrono>
#include <cmath>
#include <numeric> // iota

using namespace std;
using namespace chrono;
using Clock = chrono::steady_clock;
using Int = uint64_t;

struct Node {
	Int payload; // ignored; just for plausability.
	Node* next = nullptr;
};

static_assert(sizeof(Node) == 16, "Not 64-bit? That's OK too.");

double time(Int N, Int iters)
{
	vector<Node> memory(N);
	vector<Node*> nodes(N);
	for (Int i = 0; i < N; ++i) {
		nodes[i] = &memory[i];
	}
	std::random_shuffle(begin(nodes), end(nodes));

	// Link up the nodes:
	for (Int i = 0; i < N - 1; ++i) {
		nodes[i]->next = nodes[i + 1];
	}

	Node* start_node = nodes[0];

	nodes.clear();
	nodes.shrink_to_fit(); // Free up unused memory before meassuring:

	// Do the actual measurements:

	auto start = Clock::now();

	for (Int it = 0; it < iters; ++it) {
		// Run through all the nodes:
		Node* node = start_node;
		while (node)
		{
			node = node->next;
		}
	}

	auto dur = Clock::now() - start;
	auto ns = duration_cast<nanoseconds>(dur).count();
	return ns / double(N * iters);
}

int main()
{

	double avg = 0;
	cout << "#bytes    ns/elem" << endl;

	try 
	{
		Int stopsPerFactor = 4; // For every power of 2, how many measurements do we do?
		Int minElemensFactor = 6;  // First measurement is 2^this number of elements.
		Int maxElemsFactor = 30; // Last measurement is 2^this number of elements. 30 == 16GB of memory

		Int min = stopsPerFactor * minElemensFactor;
		Int max = stopsPerFactor * maxElemsFactor;

		for (Int ei = min; ei <= max; ++ei) 
		{
			Int N = (Int)round(pow(2.0, double(ei) / stopsPerFactor));
			//Int reps = elemsPerMeasure / N;
			Int reps = (Int)round(2e10 / pow(N, 1.5));
			if (reps < 1) reps = 1;
			auto ans = time(N, reps);
			avg = avg + ans;
			cout << (N * sizeof(Node)) << "   " << ans << "   # (N=" << N << ", reps=" << reps << ") " << (ei - min + 1) << "/" << (max - min + 1) << endl;
		}
		cout << avg / max << endl;
	}
	catch (exception& e) 
	{
		cout << "# stopped due to exception: " << e.what() << endl;
	}
	return 0;
}

编译器设置:

我在编译器设置中做错了什么?

What did I do wrong in compiler settings?

推荐答案

您好,

您确定这些都是所有编译器设置吗?通常VS有更多。

are you sure that these are really all compiler settings? Normally VS has more.

此外,你是如何将/ Ox和/ O2设置在一起的?据我所见,我只能设置其中一个。

In addition, how did you set /Ox and /O2 together? As far as I can see in my VS, you can only set one of them.

您应该打开一个新项目并将编译器设置与此项目中的设置进行比较。然后你知道,你做了哪些改变。

You should open a new project and compare the compiler settings with the ones in this project. Then you know, which changes you have done.

问候,Guido


这篇关于缓慢的C ++代码执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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