在C 11中使用静态变量是否有惩罚

前端之家收集整理的这篇文章主要介绍了在C 11中使用静态变量是否有惩罚前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在C 11中,
  1. const std::vector<int>& f() {
  2. static const std::vector<int> x { 1,2,3 };
  3. return x;
  4. }

是线程安全的.但是,由于这个额外的线程安全保证,第一次调用函数(即初始化时)是否有额外的惩罚?我想知道该函数是否比使用全局变量函数慢,因为它必须获取一个互斥体来检查每次被调用时是否被另一个线程初始化.

解决方法

“The best intution to be ever had is ‘I should measure this.'”所以 let’s find out
  1. #include <atomic>
  2. #include <chrono>
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <numeric>
  6. #include <vector>
  7.  
  8. namespace {
  9. class timer {
  10. using hrc = std::chrono::high_resolution_clock;
  11. hrc::time_point start;
  12.  
  13. static hrc::time_point now() {
  14. // Prevent memory operations from reordering across the
  15. // time measurement. This is likely overkill,needs more
  16. // research to determine the correct fencing.
  17. std::atomic_thread_fence(std::memory_order_seq_cst);
  18. auto t = hrc::now();
  19. std::atomic_thread_fence(std::memory_order_seq_cst);
  20. return t;
  21. }
  22.  
  23. public:
  24. timer() : start(now()) {}
  25.  
  26. hrc::duration elapsed() const {
  27. return now() - start;
  28. }
  29.  
  30. template <typename Duration>
  31. typename Duration::rep elapsed() const {
  32. return std::chrono::duration_cast<Duration>(elapsed()).count();
  33. }
  34.  
  35. template <typename Rep,typename Period>
  36. Rep elapsed() const {
  37. return elapsed<std::chrono::duration<Rep,Period>>();
  38. }
  39. };
  40.  
  41. const std::vector<int>& f() {
  42. static const auto x = std::vector<int>{ 1,3 };
  43. return x;
  44. }
  45.  
  46. static const auto y = std::vector<int>{ 1,3 };
  47. const std::vector<int>& g() {
  48. return y;
  49. }
  50.  
  51. const unsigned long long n_iterations = 500000000;
  52.  
  53. template <typename F>
  54. void test_one(const char* name,F f) {
  55. f(); // First call outside the timer.
  56.  
  57. using value_type = typename std::decay<decltype(f()[0])>::type;
  58. std::cout << name << ": " << std::flush;
  59.  
  60. auto t = timer{};
  61. auto sum = uint64_t{};
  62. for (auto i = n_iterations; i > 0; --i) {
  63. const auto& vec = f();
  64. sum += std::accumulate(begin(vec),end(vec),value_type{});
  65. }
  66. const auto elapsed = t.elapsed<std::chrono::milliseconds>();
  67. std::cout << elapsed << " ms (" << sum << ")\n";
  68. }
  69. } // anonymous namespace
  70.  
  71. int main() {
  72. test_one("local static",f);
  73. test_one("global static",g);
  74. }

在Coliru运行,本地版本在4618 ms中进行5e8次迭代,全局版本为4392 ms.所以是的,本地版本的迭代速度慢了大约0.452纳秒.尽管存在可衡量的差异,但是在大多数情况下,影响观察到的效果太小.

编辑:有趣的对立点,switching from clang++ to g++ changes the result ordering. g编译的二进制文件运行在4418毫秒(全局)与4181毫秒(本地),所以本地的速度比迭代速度快474皮秒.然而,它确实得出这样的结论:两种方法之间的差异很小.

编辑2:检查生成的程序集,我决定将函数指针转换为函数对象,以便更好地进行内联.通过函数指针间接调用的时间并不是OP中的代码的真正特征.所以我用这个程序:

  1. #include <atomic>
  2. #include <chrono>
  3. #include <cstdint>
  4. #include <iostream>
  5. #include <numeric>
  6. #include <vector>
  7.  
  8. namespace {
  9. class timer {
  10. using hrc = std::chrono::high_resolution_clock;
  11. hrc::time_point start;
  12.  
  13. static hrc::time_point now() {
  14. // Prevent memory operations from reordering across the
  15. // time measurement. This is likely overkill.
  16. std::atomic_thread_fence(std::memory_order_seq_cst);
  17. auto t = hrc::now();
  18. std::atomic_thread_fence(std::memory_order_seq_cst);
  19. return t;
  20. }
  21.  
  22. public:
  23. timer() : start(now()) {}
  24.  
  25. hrc::duration elapsed() const {
  26. return now() - start;
  27. }
  28.  
  29. template <typename Duration>
  30. typename Duration::rep elapsed() const {
  31. return std::chrono::duration_cast<Duration>(elapsed()).count();
  32. }
  33.  
  34. template <typename Rep,Period>>();
  35. }
  36. };
  37.  
  38. class f {
  39. public:
  40. const std::vector<int>& operator()() {
  41. static const auto x = std::vector<int>{ 1,3 };
  42. return x;
  43. }
  44. };
  45.  
  46. class g {
  47. static const std::vector<int> x;
  48. public:
  49. const std::vector<int>& operator()() {
  50. return x;
  51. }
  52. };
  53.  
  54. const std::vector<int> g::x{ 1,3 };
  55.  
  56. const unsigned long long n_iterations = 500000000;
  57.  
  58. template <typename F>
  59. void test_one(const char* name,f());
  60. test_one("global static",g());
  61. }

毫不奇怪,运行时间在g++ (3803ms local,2323ms global)clang (4183ms local,3253ms global)都更快.结果肯定了我们的直觉,全局技术应该比当地更快,每次迭代的增量为2.96纳秒(g)和1.86纳秒(克朗).

猜你在找的C&C++相关文章