c++ - 如何修复程序上的错误以衡量性能
问题描述
我想测量有关矩阵乘法的代码的性能。
虽然我能够执行一个简单的程序并得到正确的答案,但我想要得到结果的程序却无法成功编译。
我该如何修复这些错误?
我试图执行下面的简单程序来了解 C++ 中时间测量的基础。
输出
3seconds
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono;
int main()
{
// 1. current date and time
high_resolution_clock::time_point begin = high_resolution_clock::now();
// 2. process to take time
std::this_thread::sleep_for(seconds(3));
// 3. current date and time
high_resolution_clock::time_point end = high_resolution_clock::now();
// aquired passed time
seconds elapsed_time = duration_cast<seconds>(end - begin);
std::cout << elapsed_time.count() << "seconds" << std::endl;
}
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono;
int main()
{
#define N 2
double A[N][N] = {
{1.0, 2.0},
{2.0, 1.0}
};
double B[N][N] = {
{3.0, 1.0},
{0.0, 3.0}
};
double C[N][N] = {
{0.0, 0.0},
{0.0, 0.0}
};
int i, j, k;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
for(k=0; k<N; k++)
C[i][j] += A[i][k]*B[k][j];
// aquire the passed time
seconds elapsed_time = duration_cast<seconds>(end - begin);
std::cout << elapsed_time.count() << "seconds" << std::endl;
}
$ g++ -o clock clock.cpp
clock.cpp:34:49: error: use of undeclared identifier 'end'
seconds elapsed_time = duration_cast<seconds>(end - begin);
^
clock.cpp:34:55: error: use of undeclared identifier 'begin'
seconds elapsed_time = duration_cast<seconds>(end - begin);
^
2 errors generated.
解决方案
您忘记声明和初始化begin
and end
。
尝试:
#include <chrono>
#include <iostream>
#include <thread>
using namespace std::chrono;
int main()
{
#define N 2
// being
high_resolution_clock::time_point begin = high_resolution_clock::now();
double A[N][N] = {
{1.0, 2.0},
{2.0, 1.0}
};
double B[N][N] = {
{3.0, 1.0},
{0.0, 3.0}
};
double C[N][N] = {
{0.0, 0.0},
{0.0, 0.0}
};
int i, j, k;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
for(k=0; k<N; k++)
C[i][j] += A[i][k]*B[k][j];
// end
high_resolution_clock::time_point end = high_resolution_clock::now();
// aquire the passed time
seconds elapsed_time = duration_cast<seconds>(end - begin);
std::cout << elapsed_time.count() << "seconds" << std::endl;
}
推荐阅读
- knockout.js - 从服务器更新时集合对象的计算属性未更新
- mysql - phpMyAdmin 数据库导出文件大小随每个暂定而变化
- reactjs - 无法将 react-dom 15.6.1 更新到 16.xx
- jquery - .ajax() 方法的数据参数值未传递给 asp.net core 2.0 中的 C# 函数
- dump - Dcmtk 转储,通过不加载所有内容来减少时间
- python - 在 Python 中,所有变量都是空指针吗?
- shell - 如何将包含空格的变量传递给配置单元?
- c# - 为什么 CustomTaskPane 抛出空异常
- javascript - 从 jQuery 中的 ajax 请求中去除初始问号
- c++ - 在 Fedora 20 中查找简单设备驱动程序的头文件