如何修复程序上的错误以衡量性能
How to fix errors on the program to measure the performance
我想测量关于矩阵乘法的代码的性能。
虽然我能够执行一个简单的程序并得到正确的答案,但我想得到结果的程序无法成功编译。
如何修复这些错误?
我试图执行以下简单的程序来理解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
和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;
}
编译器告诉您,您正在使用两个变量而没有事先声明。在引用变量之前,必须通过指定变量的数据类型来声明变量。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- 删除一个线程上有数百万个字符串的大型哈希映射会影响另一个线程的性能
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 为什么我在编码中总是出现性能错误?
- 为什么我的性能基准给了我错误的结果?
- 避免错误共享以提高性能
- 如何修复程序上的错误以衡量性能
- 这种错误检查方法的性能会不会太高
- 对于静态错误字符串,哪个的内存/性能效率更高,或者有替代方案
- 错误的计数器路径,pdhAddCounter;窗口中的性能监视器
- openMP 性能不佳,共享错误