我从 VS2008 迁移到 VS2012,发现 VS2012 版本的性能非常缓慢。
问题在于输入流的提取运算符。istream& 运算符>> (float& val); istream& 运算符>> (double& val);
文本文件上的这些函数在 VS2012 上要慢两倍。
例如,以下代码检索 VS2012 的 3.75 秒,而 VS2008 仅检索 1.25 秒。
你能告诉我为什么吗?
提前致谢。
#include "stdafx.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <sstream>
#define FILE_NAME "D:\\test_Roadmap.txt"
const int NB_VALUE = 1000000;
const int NB_MESURE = 20;
int _tmain(int argc, _TCHAR* argv[])
{
  std::cout<<"ecriture"<<std::endl;
  //ecriture
  {
    std::ofstream ostream (FILE_NAME);
    float val = 0.f;
    for (int ii=0; ii<NB_VALUE; ii++)
    {
      ostream << val;
      ostream << " ";
      val += 0.04f;
    }
  }
  std::cout<<"lecture"<<std::endl;
  //lecture
  double texec = 0;
  for (int iMesure=0; iMesure<NB_MESURE; iMesure++)
  {
    std::ifstream istream (FILE_NAME);
    float val = 0;
    time_t tbegin1 = time(NULL);
    for (int ii=0; ii<NB_VALUE; ii++)
    {
      istream>> val;
    }
    time_t tbegin2 = time(NULL);
    texec += difftime(tbegin2,tbegin1);   
  }
  texec /= NB_MESURE;
  std::ostringstream oss1;
  oss1 << texec;
  std::string s1 = std::string(" read : ") + oss1.str() + std::string(" in s");
  std::cout<<s1<<std::endl;
  float a;
  std::cin>>a;
  return 0;
}