C ++ 튜토리얼 (b)을 측정 스레드 실행 시간 멀티 스레드

간략한 소개

멀티 스레딩을 사용하기 전에 일반적으로 시간을 측정하는 데 사용 내 첫 번째 스레드 실행 방법에 대해 말하기, 가속 달성했다. 기본적인 아이디어는 최대 실행 대표자를 포함하여 기능의 측정을 달성하는 것입니다.

기본 구문

#include<iostream>
#include<thread>
using namespace std;

using namespace std::chrono;                //增加引用空间
template<class T>
void measure(T&& func) {
	auto beg_t = system_clock::now();       //开始时间
	func();								    //执行函数
	auto end_t = system_clock::now();       //结束时间
	duration<double> diff = end_t - beg_t;
	printf("performTest total time: ");
	cout << diff.count()<<endl;
}
void func() {
	cout << "This is func thread " << endl;
	int s = 0;
	for (int i = 0; i < 5; i++)
		s += i;
}

int main() {
	measure(func);
}

결과

This is func thread
performTest total time: 0.0004911

케이스의 고급 버전 : 람다 함수의 사용이 일부 (선거 참조) 소형 실행 파일을 작성하는

int main() {
	int limit = 50;
	measure([limit]() {           //方括号捕捉外部变量limit
		cout << "This is func thread " << endl;
		int s = 0;
		for (int i = 0; i < limit; i++)
			s += i;
		}
	);
}
void measure(T&& func){
	auto beg_t = system_clock::now();
	func();
	auto end_t = system_clock::now();
	duration<double> diff = end_t - beg_t;
	printf("PerformTest total time: ");
	cout << diff.count()<<endl;
}

개요

이 장에서는 실행 시간 측정을 측정하는 기능을 사용하는 방법을 설명, 다음 절에서는 특정 프로세스가 가속화 될 대해 설명합니다.

참고 자료

링크 : CPP 작은 지식 --lambda 표현 .
링크 : 시간의 팁 일반적인 측정 기능 .

게시 27 개 원래 기사 · 원 찬양 10 ·은 30000 +를 볼

추천

출처blog.csdn.net/tiaojingtao1293/article/details/104704700