C ++ 프로그램에서 특정 함수가 Linux 에서 실행되는 데 걸리는 시간을 알고 싶습니다 . 그 후 속도 비교를하고 싶습니다. 나는 몇 가지 시간 기능을 보았지만 부스트에서 이것으로 끝났습니다. 크로노 :
process_user_cpu_clock, captures user-CPU time spent by the current process
이제 위의 기능을 사용하는지 확실하지 않습니다. CPU가 해당 기능에 소비 한 유일한 시간을 얻을 수 있습니까?
둘째, 위의 기능을 사용하는 예를 찾지 못했습니다. 위의 기능을 사용하는 방법을 알려주시겠습니까?
추신 : 지금 std::chrono::system_clock::now()
은 초 단위로 시간을 사용 하고 있지만 매번 다른 CPU로드로 인해 다른 결과를 얻습니다.
답변
C ++ 11에서 사용하기 매우 쉬운 방법입니다. 당신은 사용해야 std::chrono::high_resolution_clock
에서 <chrono>
헤더입니다.
다음과 같이 사용하십시오.
#include <iostream>
#include <chrono>
void function()
{
long long number = 0;
for( long long i = 0; i != 2000000; ++i )
{
number += 5;
}
}
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
function();
auto t2 = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
std::cout << duration;
return 0;
}
기능의 지속 시간을 측정합니다.
참고 : 기능의 타이밍이 항상 같은 것은 아닙니다. 이는 수학 연습을 풀 때 마음이 다소 집중 될 수있는 것처럼 컴퓨터의 CPU가 컴퓨터에서 실행중인 다른 프로세스에 의해 사용되거나 사용되지 않을 수 있기 때문입니다. 인간의 마음에는 수학 문제의 해결책을 기억할 수 있지만 컴퓨터의 경우 동일한 프로세스가 항상 새로운 것입니다. 따라서 내가 말했듯이 항상 동일한 결과를 얻지는 못할 것입니다!
답변
다음은 인수로 전달 된 함수의 실행 시간을 측정하는 함수입니다.
#include <chrono>
#include <utility>
typedef std::chrono::high_resolution_clock::time_point TimeVar;
#define duration(a) std::chrono::duration_cast<std::chrono::nanoseconds>(a).count()
#define timeNow() std::chrono::high_resolution_clock::now()
template<typename F, typename... Args>
double funcTime(F func, Args&&... args){
TimeVar t1=timeNow();
func(std::forward<Args>(args)...);
return duration(timeNow()-t1);
}
사용법 예 :
#include <iostream>
#include <algorithm>
typedef std::string String;
//first test function doing something
int countCharInString(String s, char delim){
int count=0;
String::size_type pos = s.find_first_of(delim);
while ((pos = s.find_first_of(delim, pos)) != String::npos){
count++;pos++;
}
return count;
}
//second test function doing the same thing in different way
int countWithAlgorithm(String s, char delim){
return std::count(s.begin(),s.end(),delim);
}
int main(){
std::cout<<"norm: "<<funcTime(countCharInString,"precision=10",'=')<<"\n";
std::cout<<"algo: "<<funcTime(countWithAlgorithm,"precision=10",'=');
return 0;
}
산출:
norm: 15555
algo: 2976
답변
함수 실행 시간을 찾는 간단한 프로그램.
#include <iostream>
#include <ctime> // time_t
#include <cstdio>
void function()
{
for(long int i=0;i<1000000000;i++)
{
// do nothing
}
}
int main()
{
time_t begin,end; // time_t is a datatype to store time values.
time (&begin); // note time before execution
function();
time (&end); // note time after execution
double difference = difftime (end,begin);
printf ("time taken for function() %.2lf seconds.\n", difference );
return 0;
}
답변
Scott Meyers 책에서 함수 실행 시간을 측정하는 데 사용할 수있는 범용 일반 람다 식의 예를 찾았습니다. (C ++ 14)
auto timeFuncInvocation =
[](auto&& func, auto&&... params) {
// get time before function invocation
const auto& start = std::chrono::high_resolution_clock::now();
// function invocation using perfect forwarding
std::forward<decltype(func)>(func)(std::forward<decltype(params)>(params)...);
// get time after function invocation
const auto& stop = std::chrono::high_resolution_clock::now();
return stop - start;
};
문제는 하나의 실행 만 측정하므로 결과가 매우 다를 수 있다는 것입니다. 안정적인 결과를 얻으려면 많은 실행을 측정해야합니다. 코드 : : 다이브 2015 컨퍼런스에서 Andrei Alexandrescu 강의에 따르면-빠른 코드 작성 :
측정 시간 : tm = t + tq + tn + to
어디:
tm-측정 된 (관찰 된) 시간
t-실제 관심 시간
tq-양자화 노이즈로 추가 된 시간
tn-다양한 소음원에 의해 추가 된 시간
-오버 헤드 시간 (측정, 루핑, 호출 함수)
그가 강의 후반에 말한 것에 따르면, 당신은 그 결과로이 많은 수의 집행을 최소한으로 취해야합니다. 그 이유를 설명하는 강의를 살펴 보시기 바랍니다.
또한 google- https ://github.com/google/benchmark의 매우 훌륭한 라이브러리가 있습니다 . 이 라이브러리는 사용하기 매우 간단하고 강력합니다. Chandler Carruth의 강의를 YouTube에서 실제로이 라이브러리를 사용하고있는 YouTube에서 확인할 수 있습니다. 예를 들어 CppCon 2017 : Chandler Carruth“가는 곳이 더 빠릅니다”;
사용법 예 :
#include <iostream>
#include <chrono>
#include <vector>
auto timeFuncInvocation =
[](auto&& func, auto&&... params) {
// get time before function invocation
const auto& start = high_resolution_clock::now();
// function invocation using perfect forwarding
for(auto i = 0; i < 100000/*largeNumber*/; ++i) {
std::forward<decltype(func)>(func)(std::forward<decltype(params)>(params)...);
}
// get time after function invocation
const auto& stop = high_resolution_clock::now();
return (stop - start)/100000/*largeNumber*/;
};
void f(std::vector<int>& vec) {
vec.push_back(1);
}
void f2(std::vector<int>& vec) {
vec.emplace_back(1);
}
int main()
{
std::vector<int> vec;
std::vector<int> vec2;
std::cout << timeFuncInvocation(f, vec).count() << std::endl;
std::cout << timeFuncInvocation(f2, vec2).count() << std::endl;
std::vector<int> vec3;
vec3.reserve(100000);
std::vector<int> vec4;
vec4.reserve(100000);
std::cout << timeFuncInvocation(f, vec3).count() << std::endl;
std::cout << timeFuncInvocation(f2, vec4).count() << std::endl;
return 0;
}
편집 : 물론 컴파일러는 무언가를 최적화 할 수 있다는 것을 항상 기억해야합니다. 이러한 경우 perf와 같은 도구가 유용 할 수 있습니다.
답변
이전 C ++ 또는 C를위한 쉬운 방법 :
#include <time.h> // includes clock_t and CLOCKS_PER_SEC
int main() {
clock_t start, end;
start = clock();
// ...code to measure...
end = clock();
double duration_sec = double(end-start)/CLOCKS_PER_SEC;
return 0;
}
초 단위의 타이밍 정밀도는 1.0/CLOCKS_PER_SEC
답변
- C ++ 11에서 메소드를 사용하는 것은 매우 쉽습니다.
- 헤더에서 std :: chrono :: high_resolution_clock을 사용할 수 있습니다
- 메소드 실행 시간을 훨씬 읽기 쉬운 형식으로 인쇄하는 메소드를 작성할 수 있습니다.
예를 들어 1에서 1 억 사이의 모든 소수를 찾으려면 약 1 분 40 초가 걸립니다. 따라서 실행 시간은 다음과 같이 인쇄됩니다.
Execution Time: 1 Minutes, 40 Seconds, 715 MicroSeconds, 715000 NanoSeconds
코드는 다음과 같습니다.
#include <iostream>
#include <chrono>
using namespace std;
using namespace std::chrono;
typedef high_resolution_clock Clock;
typedef Clock::time_point ClockTime;
void findPrime(long n, string file);
void printExecutionTime(ClockTime start_time, ClockTime end_time);
int main()
{
long n = long(1E+8); // N = 100 million
ClockTime start_time = Clock::now();
// Write all the prime numbers from 1 to N to the file "prime.txt"
findPrime(n, "C:\\prime.txt");
ClockTime end_time = Clock::now();
printExecutionTime(start_time, end_time);
}
void printExecutionTime(ClockTime start_time, ClockTime end_time)
{
auto execution_time_ns = duration_cast<nanoseconds>(end_time - start_time).count();
auto execution_time_ms = duration_cast<microseconds>(end_time - start_time).count();
auto execution_time_sec = duration_cast<seconds>(end_time - start_time).count();
auto execution_time_min = duration_cast<minutes>(end_time - start_time).count();
auto execution_time_hour = duration_cast<hours>(end_time - start_time).count();
cout << "\nExecution Time: ";
if(execution_time_hour > 0)
cout << "" << execution_time_hour << " Hours, ";
if(execution_time_min > 0)
cout << "" << execution_time_min % 60 << " Minutes, ";
if(execution_time_sec > 0)
cout << "" << execution_time_sec % 60 << " Seconds, ";
if(execution_time_ms > 0)
cout << "" << execution_time_ms % long(1E+3) << " MicroSeconds, ";
if(execution_time_ns > 0)
cout << "" << execution_time_ns % long(1E+6) << " NanoSeconds, ";
}
답변
다음은 함수 또는 코드 블록의 경과 시간을 측정하기위한 우수한 헤더 전용 클래스 템플릿입니다.
#ifndef EXECUTION_TIMER_H
#define EXECUTION_TIMER_H
template<class Resolution = std::chrono::milliseconds>
class ExecutionTimer {
public:
using Clock = std::conditional_t<std::chrono::high_resolution_clock::is_steady,
std::chrono::high_resolution_clock,
std::chrono::steady_clock>;
private:
const Clock::time_point mStart = Clock::now();
public:
ExecutionTimer() = default;
~ExecutionTimer() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Destructor Elapsed: "
<< std::chrono::duration_cast<Resolution>( end - mStart ).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
inline void stop() {
const auto end = Clock::now();
std::ostringstream strStream;
strStream << "Stop Elapsed: "
<< std::chrono::duration_cast<Resolution>(end - mStart).count()
<< std::endl;
std::cout << strStream.str() << std::endl;
}
}; // ExecutionTimer
#endif // EXECUTION_TIMER_H
다음은 그 사용법입니다.
int main() {
{ // empty scope to display ExecutionTimer's destructor's message
// displayed in milliseconds
ExecutionTimer<std::chrono::milliseconds> timer;
// function or code block here
timer.stop();
}
{ // same as above
ExecutionTimer<std::chrono::microseconds> timer;
// code block here...
timer.stop();
}
{ // same as above
ExecutionTimer<std::chrono::nanoseconds> timer;
// code block here...
timer.stop();
}
{ // same as above
ExecutionTimer<std::chrono::seconds> timer;
// code block here...
timer.stop();
}
return 0;
}
수업은 템플릿이기 때문에 시간을 측정하고 표시하는 방법을 쉽게 지정할 수 있습니다. 이것은 벤치 마킹을 수행하기위한 매우 유용한 유틸리티 클래스 템플릿이며 사용하기 매우 쉽습니다.