C ++最快的“最终" [英] Fastest `finally` for C++
问题描述
C ++不支持finally
子句用于try
语句.这导致对如何释放资源的猜测.在互联网上研究了这个问题之后,尽管我找到了一些解决方案,但是我对它们的性能并不清楚(如果性能没那么重要,我会使用Java).所以我必须进行基准测试.
C++ so far (unfortunately) doesn't support finally
clause for a try
statement. This leads to speculations on how to release resources. After studying the question on the internet, although I found some solutions, I didn't get clear about their performance (and I would use Java if performance didn't matter that much). So I had to benchmark.
这些选项是:
-
在 CodeProject上提出的
-
基于Functor的
finally
类 .它功能强大,但速度较慢.反汇编表明,捕获外部函数的局部变量的效率非常低:将它们逐个推送到堆栈,而不是仅将帧指针传递给内部(lambda)函数.
Functor-based
finally
class proposed at CodeProject. It's powerful, but slow. And the disassembly suggests that outer function local variables are captured very inefficiently: pushed to the stack one by one, rather than passing just the frame pointer to the inner (lambda) function.
RAII:堆栈上的手动清洁器对象:缺点是手动键入并针对每个使用的位置进行定制.另一个缺点是需要将释放资源所需的所有变量复制到其中.
RAII: Manual cleaner object on the stack: the disadvantage is manual typing and tailoring it for each place used. Another disadvantage is the need to copy to it all the variables needed for resource release.
特定于MSVC ++的 __try
/
MSVC++ specific __try
/ __finally
statement. The disadvantage is that it's obviously not portable.
我创建了这个小型基准测试来比较这些方法的运行时性能:
I created this small benchmark to compare the runtime performance of these approaches:
#include <chrono>
#include <functional>
#include <cstdio>
class Finally1 {
std::function<void(void)> _functor;
public:
Finally1(const std::function<void(void)> &functor) : _functor(functor) {}
~Finally1() {
_functor();
}
};
void BenchmarkFunctor() {
volatile int64_t var = 0;
const int64_t nIterations = 234567890;
auto start = std::chrono::high_resolution_clock::now();
for (int64_t i = 0; i < nIterations; i++) {
Finally1 doFinally([&] {
var++;
});
}
auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nSec = 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
printf("Functor: %.3lf Ops/sec, var=%lld\n", nIterations / nSec, (long long)var);
}
void BenchmarkObject() {
volatile int64_t var = 0;
const int64_t nIterations = 234567890;
auto start = std::chrono::high_resolution_clock::now();
for (int64_t i = 0; i < nIterations; i++) {
class Cleaner {
volatile int64_t* _pVar;
public:
Cleaner(volatile int64_t& var) : _pVar(&var) { }
~Cleaner() { (*_pVar)++; }
} c(var);
}
auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nSec = 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
printf("Object: %.3lf Ops/sec, var=%lld\n", nIterations / nSec, (long long)var);
}
void BenchmarkMSVCpp() {
volatile int64_t var = 0;
const int64_t nIterations = 234567890;
auto start = std::chrono::high_resolution_clock::now();
for (int64_t i = 0; i < nIterations; i++) {
__try {
}
__finally {
var++;
}
}
auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nSec = 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
printf("__finally: %.3lf Ops/sec, var=%lld\n", nIterations / nSec, (long long)var);
}
template <typename Func> class Finally4 {
Func f;
public:
Finally4(Func&& func) : f(std::forward<Func>(func)) {}
~Finally4() { f(); }
};
template <typename F> Finally4<F> MakeFinally4(F&& f) {
return Finally4<F>(std::forward<F>(f));
}
void BenchmarkTemplate() {
volatile int64_t var = 0;
const int64_t nIterations = 234567890;
auto start = std::chrono::high_resolution_clock::now();
for (int64_t i = 0; i < nIterations; i++) {
auto doFinally = MakeFinally4([&] { var++; });
//Finally4 doFinally{ [&] { var++; } };
}
auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nSec = 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
printf("Template: %.3lf Ops/sec, var=%lld\n", nIterations / nSec, (long long)var);
}
void BenchmarkEmpty() {
volatile int64_t var = 0;
const int64_t nIterations = 234567890;
auto start = std::chrono::high_resolution_clock::now();
for (int64_t i = 0; i < nIterations; i++) {
var++;
}
auto elapsed = std::chrono::high_resolution_clock::now() - start;
double nSec = 1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
printf("Empty: %.3lf Ops/sec, var=%lld\n", nIterations / nSec, (long long)var);
}
int __cdecl main() {
BenchmarkFunctor();
BenchmarkObject();
BenchmarkMSVCpp();
BenchmarkTemplate();
BenchmarkEmpty();
return 0;
}
在我的Ryzen 1800X @ 3.9Ghz和DDR4 @ 2.6Ghz CL13上的结果是:
The results on my Ryzen 1800X @3.9Ghz with DDR4 @2.6Ghz CL13 were:
Functor: 175148825.946 Ops/sec, var=234567890
Object: 553446751.181 Ops/sec, var=234567890
__finally: 553832236.221 Ops/sec, var=234567890
Template: 554964345.876 Ops/sec, var=234567890
Empty: 554468478.903 Ops/sec, var=234567890
显然,除基于函子(#1)之外的所有选项都与空循环一样快.
Apparently, all the options except functor-base (#1) are as fast as an empty loop.
那么,有没有一种快速而强大的C ++替代品finally
,它可以移植并且需要从外部函数堆栈中进行最少的复制?
So is there a fast and powerful C++ alternative to finally
, which is portable and requires minimum copying from the stack of the outer function?
更新:我已经对@ Jarod42解决方案进行了基准测试,因此问题中的此处是更新的代码和输出.尽管如@Sopel所述,但如果不执行复制省略,则可能会中断.
UPDATE: I've benchmarked @Jarod42 solution, so here in the question is updated code and output. Though as mentioned by @Sopel, it may break if copy elision is not performed.
UPDATE2:澄清我要的是C ++中即使抛出异常也执行代码块的便捷方法.由于问题中提到的原因,某些方法比较缓慢或不便.
UPDATE2: To clarify what I'm asking for is a convenient fast way in C++ to execute a block of code even if an exception is thrown. For the reasons mentioned in the question, some ways are slow or inconvenient.
推荐答案
您可以实现Finally
而无需擦除类型和std::function
的开销:
You can implement Finally
without type erasure and overhead of std::function
:
template <typename F>
class Finally {
F f;
public:
template <typename Func>
Finally(Func&& func) : f(std::forward<Func>(func)) {}
~Finally() { f(); }
Finally(const Finally&) = delete;
Finally(Finally&&) = delete;
Finally& operator =(const Finally&) = delete;
Finally& operator =(Finally&&) = delete;
};
template <typename F>
Finally<F> make_finally(F&& f)
{
return { std::forward<F>(f) };
}
并像这样使用它:
auto&& doFinally = make_finally([&] { var++; });
这篇关于C ++最快的“最终"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!