C++ 中的时间测试

在各种 oj  平台做题的时候,经常会出现时间要求,即你的算法复杂度一般不能太高,否则会超时。

同时,在CCF考试中,练超时都不会告诉你,考试的时候不会出程序结果,因此,可以自己测一下运行时间,可以使用以下代码来完成。

#include<iostream>
#include<ctime>

using namespace std;

int main()
{
	clock_t start,finish;
	start=clock();
    //个人代码
	finish=clock();
	cout<<(double)(finish-start)/CLOCKS_PER_SEC<<"(s)"<<endl;
}

C++ 中的空间申请

当申请空间比较大的时候,直接开静态数组程序可能会崩掉。

比如说如下

#include<iostream>
using namespace std;
const int MAX_N = 300000000;

int a[MAX_N],b[MAX_N];
int main()
{
	int n;
	cin >> n;
	cout << n << endl;
}

vs会报错

错误    LNK1248    映像大小(8F826000)超过允许的最大大小(80000000)       

Dev也会报错

C++ 中的时间测试与空间申请

总而言之,就是你申请的空间太大了,所以可以通过动态数组(申请栈空间)来实现

C++ 中的时间测试与空间申请

这样再运行就没问题了。

相关文章:

  • 2022-12-23
  • 2021-06-29
  • 2022-02-23
  • 2021-06-01
  • 2021-10-31
  • 2021-06-11
  • 2021-11-21
猜你喜欢
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-06
相关资源
相似解决方案