이전에는 함수 호출이 신속하게 확인하고 싶을 때마다 매크로를 사용하여 측정했습니다. 이제 가능한 C++ 11, 나는 마지막으로이 같은 뭔가 처리기 코드의 추악한 평화를 제거하고 교체하고 싶습니다 : 뭔가 (즉하지 void
)을 반환하는 함수에 대해 잘 작동void 및 non-void 반환 함수에 대한 완벽한 전달
template <typename Functor, typename ... Args>
auto measure(Functor f, Args && ... args)
-> decltype(f(std::forward<Args>(args)...))
{
auto now = std::chrono::high_resolution_clock::now();
auto ret = f(std::forward<Args>(args)...);
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - now).count();
std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
return ret;
}
합니다. 그래서 void
함수에 과부하가 필요하다고 느꼈으 나 반환 유형에만 함수를 오버로드 할 수는 없습니다.
나는이 템플릿을 사용하여이 문제를 해결하려고 노력했지만 아무 소용이 없었다.
template <
typename Functor, typename ... Args,
typename ReturnType = typename std::enable_if<
!std::is_void<
typename std::result_of<Functor(Args...)>::type
>::value,
typename std::result_of<Functor(Args...)>::type
>::type
>
ReturnType measure(Functor f, Args && ... args)
{
auto now = std::chrono::high_resolution_clock::now();
auto ret = f(std::forward<Args>(args)...);
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - now).count();
std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
return ret;
}
template <
typename Functor, typename ... Args,
typename ReturnType = typename std::enable_if<
std::is_void<
typename std::result_of<Functor(Args...)>::type
>::value
>::type
>
ReturnType measure(Functor f, Args && ... args)
{
auto now = std::chrono::high_resolution_clock::now();
f(std::forward<Args>(args)...);
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - now).count();
std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
}
이 주위에 방법이 있나요 : 컴파일러는 여전히 기능 measure
가 두 번 정의 불평?
template <typename Functor, typename ... Args>
auto measure(Functor f, Args && ... args)
-> decltype(f(std::forward<Args>(args)...))
{
struct scoped_timer
{
scoped_timer() : now_(std::chrono::high_resolution_clock::now()) {}
~scoped_timer()
{
auto elapsed = std::chrono::duration_cast<
std::chrono::milliseconds
>(std::chrono::high_resolution_clock::now() - now_).count();
std::cout << "Time elapsed: " << elapsed << "ms" << std::endl;
}
private:
std::chrono::high_resolution_clock::time_point const now_;
} scoped_timer;
return f(std::forward<Args>(args)...);
}
http://flamingdangerzone.com/cxx11/2012/06/01/almost-static-if.html#evolution –
우아한 [아이디어] (http://stackoverflow.com/a/17748197/1137388) ([R. Martinho Fernandes] (http://stackoverflow.com/users/46642/r-martinho-fernandes)). 유일한 변경은 '~ scoped_timer()'의 코드를'try-catch' 블록에 넣는 것입니다. 의미 상으로는,'f'가 성공적으로 완료되지 않으면 실행되는 시간을보고하지 않는 것이 합리적이라고 생각합니다. 불행하게도 이것은'<< "에 의해 던져 질 가능성이있는 예외에 대해서는 그리 명백하지 않습니다. 오래된'printf'가 (예외 안전에 관한) 더 나은 대안이 될 것입니까? 나는 모른다. –