我尝试构建一个函数模板,可以测量任意类型函数的执行时间.这是我到目前为止所尝试的:
#include <chrono>
#include <iostream>
#include <type_traits>
#include <utility>
// Executes fn with arguments args and returns the time needed
// and the result of f if it is not void
template <class Fn, class... Args>
auto timer(Fn fn, Args... args)
-> std::pair<double, decltype(fn(args...))> {
static_assert(!std::is_void<decltype(fn(args...))>::value,
"Call timer_void if return type is void!");
auto start = std::chrono::high_resolution_clock::now();
auto ret = fn(args...);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
return { elapsed_seconds.count(), ret };
}
// If fn …Run Code Online (Sandbox Code Playgroud) 我感兴趣的是计算自由函数或成员函数(模板与否)的执行时间.调用TheFunc有问题的函数,它的调用是
TheFunc(/*parameters*/);
Run Code Online (Sandbox Code Playgroud)
要么
ReturnType ret = TheFunc(/*parameters*/);
Run Code Online (Sandbox Code Playgroud)
当然我可以按如下方式包装这些函数调用:
double duration = 0.0 ;
std::clock_t start = std::clock();
TheFunc(/*parameters*/);
duration = static_cast<double>(std::clock() - start) / static_cast<double>(CLOCKS_PER_SEC);
Run Code Online (Sandbox Code Playgroud)
要么
double duration = 0.0 ;
std::clock_t start = std::clock();
ReturnType ret = TheFunc(/*parameters*/);
duration = static_cast<double>(std::clock() - start) / static_cast<double>(CLOCKS_PER_SEC);
Run Code Online (Sandbox Code Playgroud)
但是我想做一些比这更优雅的事情,即(从现在起我将坚持使用void返回类型)如下:
Timer thetimer ;
double duration = 0.0;
thetimer(*TheFunc)(/*parameters*/, duration);
Run Code Online (Sandbox Code Playgroud)
其中Timer是我想设计的一些时序类,这将允许我编写前面的代码,这样在前面代码的最后一行的exectution之后,double duration将包含执行时间
TheFunc(/*parameters*/);
Run Code Online (Sandbox Code Playgroud)
但我不知道如何做到这一点,也不是我的目标语法/解决方案是最佳的...
对于一个项目,我应该测量我正在使用的机器在C++中的基本原始操作的运行时间.它说:
写一个程序确定的(定时参数的值
fetch,store,+,-,*,/,<,函数调用,函数返回,new,delete,和[]),用于在其上运行的机器.
fetch并store是:
a = b + 1
Run Code Online (Sandbox Code Playgroud)
b和1是被"取出"(以及加入连同+),并存储在a.
我以前从未做过这样的事情.我需要使用时钟方法来计算运行时间吗?我的时间代码应该复杂还是简单?应该评估多少次?
假设我有一个测量类的时间,可以通过这样的持续时间类型进行参数化
template<typename TimeT = std::chrono::milliseconds>
struct measure
{ /* implementation */ };
Run Code Online (Sandbox Code Playgroud)
我想要的是能够打印出来的TimeT.我倾向于实现这样的静态成员函数:
static string TimeType() const;
Run Code Online (Sandbox Code Playgroud)
我的问题是:
typeinfo / name组合(在这种情况下我必须删除constexpr上面的内容)或者我应该选择创建几个特殊化,这些特化会在每种类型时返回正确的字符串吗?为什么<chrono>Google CPP指南中有未经批准的标题?我在Google CPP风格指南中找不到任何直接提及的内容. 这一点提到可移植性问题与<ratio>和<cfenv>,但只字未提<chrono>.
任务是Processfunction()每x(例如x = 10)秒执行一个函数(例如)。
使用下面的代码,我能够Processfunction()每x秒调用一次。
问题:如何处理函数需要10秒钟以上才能完成执行的情况?
一种方法是有一个标志来指示Processfunction()执行结束并在调用之前检查它Processfunction()。有一个更好的方法吗 ?
#include <pthread.h>
#include <unistd.h> // for sleep() and usleep()
void *timerthread(void *timer_parms) {
struct itimerspec new_value;
int max_exp, fd;
struct timespec now;
uint64_t exp;
ssize_t s;
struct timer_params *p =(struct timer_params*)timer_parms;
printf("starttimer Start\n");
/* Create a CLOCK_REALTIME absolute timer with initial
expiration and interval as specified in command line */
if (clock_gettime(CLOCK_REALTIME, &now) == -1)
handle_error("clock_gettime");
new_value.it_value.tv_sec = now.tv_sec;
new_value.it_value.tv_nsec = now.tv_nsec + p->tv_nsec; …Run Code Online (Sandbox Code Playgroud) 我很好奇许多字符串连接的性能.在C++中阅读了Efficient string concatenation之后,我做了一些测试.但结果因编译器不同而不同.
这是我的代码.(代码中的计时器来自此处.)
#include <iostream>
#include <sstream>
#include <string>
#include <chrono>
template<typename TimeT = std::chrono::milliseconds>
struct measure {
template<typename F, typename ...Args>
static typename TimeT::rep execution(F&& func, Args&&... args) {
auto start = std::chrono::system_clock::now();
std::forward<decltype(func)>(func)(std::forward<Args>(args)...);
auto duration = std::chrono::duration_cast< TimeT>
(std::chrono::system_clock::now() - start);
return duration.count();
}
};
std::string strAppend(const std::string &s, int cnt) {
std::string str;
for (int i = 0; i < cnt; ++i)
str.append(s);
return str;
}
std::string strOp(const std::string &s, …Run Code Online (Sandbox Code Playgroud) 我的目标是读取默认OpenGL帧缓冲区的内容并将像素数据存储在a中cv::Mat.显然有两种不同的方法可以实现这一目标:
1)同步:使用FBO和 glRealPixels
cv::Mat a = cv::Mat::zeros(cv::Size(1920, 1080), CV_8UC3);
glReadPixels(0, 0, 1920, 1080, GL_BGR, GL_UNSIGNED_BYTE, a.data);
Run Code Online (Sandbox Code Playgroud)
2)异步:使用PBO和 glReadPixels
cv::Mat b = cv::Mat::zeros(cv::Size(1920, 1080), CV_8UC3);
glBindBuffer(GL_PIXEL_PACK_BUFFER, pbo_userImage);
glReadPixels(0, 0, 1920, 1080, GL_BGR, GL_UNSIGNED_BYTE, 0);
unsigned char* ptr = static_cast<unsigned char*>(glMapBuffer(GL_PIXEL_PACK_BUFFER, GL_READ_ONLY));
std::copy(ptr, ptr + 1920 * 1080 * 3 * sizeof(unsigned char), b.data);
glUnmapBuffer(GL_PIXEL_PACK_BUFFER);
glBindBuffer(GL_PIXEL_PACK_BUFFER, 0);
Run Code Online (Sandbox Code Playgroud)
根据我在此主题上收集的所有信息,异步版本2)应该更快.但是,比较两个版本的经过时间会产生差异通常是最小的,有时版本1)事件优于PBO变体.
对于性能检查,我插入了以下代码(基于此答案):
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
....
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end …Run Code Online (Sandbox Code Playgroud)