C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c – 多线程性能std :: string大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我们在使用OpenMP的项目上运行一些代码,我遇到了一些奇怪的事情.我已经包含了一些演示代码的部分内容,以展示我所看到的内容.

测试比较在多线程循环中使用带有std :: String参数的const char *参数调用函数.这些函数基本上什么都不做,所以没有开销.

我所看到的是完成循环所需时间的主要差异.对于执行100,000,000次迭代的const char *版本,代码需要0.075秒才能完成,而std :: String版本需要5.08秒.这些测试是在带有gcc-4.4的Ubuntu-10.04-x64上完成的.

我的问题基本上是否这完全是由于std :: String的动态分配以及为什么在这种情况下无法优化掉,因为它是const并且不能改变?

以下代码,非常感谢您的回复.

编译:g -Wall -Wextra -O3 -fopenmp String_args.cpp -o String_args

#include <iostream>
#include <map>
#include <String>
#include <stdint.h>

// For wall time
#ifdef _WIN32
#include <time.h>
#else
#include <sys/time.h>
#endif

namespace
{
  const int64_t g_max_iter = 100000000;
  std::map<const char*,int> g_charIndex = std::map<const char*,int>();
  std::map<std::string,int> g_Strindex = std::map<std::string,int>();

  class Timer
  {
  public:
    Timer()
    {
    #ifdef _WIN32
      m_start = clock();
    #else /* linux & mac */
      gettimeofday(&m_start,0);
    #endif
    }

    float elapsed()
    {
    #ifdef _WIN32
      clock_t Now = clock();
      const float retval = float(Now - m_start)/CLOCKS_PER_SEc;
      m_start = Now;
    #else /* linux & mac */
      timeval Now;
      gettimeofday(&Now,0);
      const float retval = float(Now.tv_sec - m_start.tv_seC) + float((Now.tv_usec - m_start.tv_useC)/1E6);
      m_start = Now;
    #endif
      return retval;
    }

  private:
    // The type of this variable is different depending on the platform
#ifdef _WIN32
    clock_t
#else
    timeval
#endif
    m_start;   ///< The starTing time (implementation dependent format)
  };

}

bool contains_char(const char * id)
{
  if( g_charIndex.empty() ) return false;
  return (g_charIndex.find(id) != g_charIndex.end());
}

bool contains_str(const std::string & Name)
{
  if( g_Strindex.empty() ) return false;
  return (g_Strindex.find(Name) != g_Strindex.end());
}

void do_serial_char()
{
  int found(0);
  Timer clock;
  for( int64_t i = 0; i < g_max_iter; ++i )
  {
    if( contains_char("pos") )
    {
     ++found;
    }
  }
  std::cout << "Loop time: " << clock.elapsed() << "\n";
  ++found;
}

void do_parallel_char()
{
  int found(0);
  Timer clock;
#pragma omp parallel for
  for( int64_t i = 0; i < g_max_iter; ++i )
  {
    if( contains_char("pos") )
    {
     ++found;
    }
  }
  std::cout << "Loop time: " << clock.elapsed() << "\n";
  ++found;
}

void do_serial_str()
{
  int found(0);
  Timer clock;
  for( int64_t i = 0; i < g_max_iter; ++i )
  {
    if( contains_str("pos") )
    {
     ++found;
    }
  }
  std::cout << "Loop time: " << clock.elapsed() << "\n";
  ++found;
}

void do_parallel_str()
{
  int found(0);
  Timer clock;
#pragma omp parallel for
  for( int64_t i = 0; i < g_max_iter ; ++i )
  {
    if( contains_str("pos") )
    {
     ++found;
    }
  }
  std::cout << "Loop time: " << clock.elapsed() << "\n";
  ++found;
}

int main()
{
  std::cout << "StarTing single-threaded loop using std::string\n";
  do_serial_str();
  std::cout << "\nStarTing multi-threaded loop using std::string\n";
  do_parallel_str();

  std::cout << "\nStarTing single-threaded loop using char *\n";
  do_serial_char();
  std::cout << "\nStarTing multi-threaded loop using const char*\n";
  do_parallel_char();
  }

解决方法

是的,这是由于每次迭代时std :: String的分配和复制.

@L_502_1@可能会对此进行优化,但目前的优化器不太可能发生这种情况.相反,你可以自己提升弦:

void do_parallel_str()
{
  int found(0);
  Timer clock;
  std::string const str = "pos";  // you can even make it static,if Desired
#pragma omp parallel for
  for( int64_t i = 0; i < g_max_iter; ++i )
  {
    if( contains_str(str) )
    {
      ++found;
    }
  }
  //clock.stop();  // Or use something to that affect,so you don't include
  // any of the below expression (such as outpuTing "Loop time: ") in the timing.
  std::cout << "Loop time: " << clock.elapsed() << "\n";
  ++found;
}

大佬总结

以上是大佬教程为你收集整理的c – 多线程性能std :: string全部内容,希望文章能够帮你解决c – 多线程性能std :: string所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。