2014-11-27 2 views
0

-15에서 -15 사이의 값을 갖는 큰 행렬이 있습니다. 아래 함수를 사용하여 텍스트 파일에 쓰고 싶습니다. 쓰기 속도는 약 0.1MB/s 인 것으로 보이므로 어떤 결과없이 더 빠르게 만들 수 있는지 알아보기 위해 약간 놀았습니다. 어떻게하면 더 빨리 만들 수 있습니까? 당신이 코멘트에서 촬영C++에서 텍스트 파일을 빨리 쓰십시오.

bool mymap::write_one_mat(string txtfile, matrix& mat) 
{ 
ofstream myfile (txtfile, ios::app|ios::binary); 

int element; 

if (myfile.is_open()) 
{ 
    int rows = mat.get_rows(); 
    int cols = mat.get_cols(); 
    myfile << "<"; 
    for(int i = 1; i <= rows; ++i) 
    { 
     for(int j = 1; j <= cols; ++j) 
     { 
      element = mat.get_element(i,j); 
      if(element < 0 || element > 9) 
      { 
       myfile << to_string(element); 
      } 
      else 
      { 
       myfile << " "; 
       myfile << to_string(element); 
      } 
     } 
    } 

    myfile << ">\n"; 

    myfile.close(); 
    return true; 
} 
else 
    return false; 
} 
+1

여기서 'to_string'을 사용하는 이유는 오버로드 된 스트림 연산자가 변환을 처리하고 아마도 불필요한 할당을 처리하지 않기 때문입니다. –

+4

이 질문은 코드 검토이기 때문에 주제가 아닌 것처럼 보입니다. – 0x499602D2

+0

두 브랜치 아래 두 번에'myfile << to_string (element);를 쓸 필요는 없습니다. 'myfile << "";을 써야만하며,'else' 브랜치는 필요 없습니다. – 0x499602D2

답변

0

: txtfile의 및 mat의 유형 const에 대한 참조로 변경되었음을 추가로

bool mymap::write_one_mat(std::string const& txtfile, matrix const& mat) 
{ 
    std::ios_base::sync_with_stdio(false); 
    std::ofstream myfile(txtfile, std::ios_base::app | std::ios_base::binary); 

    if (myfile.is_open()) 
    { 
     int rows = mat.get_rows(); 
     int cols = mat.get_cols(); 

     myfile << "<"; 
     for (int i = 1; i <= rows; ++i) 
     { 
      for (int j = 1; j <= cols; ++j) 
      { 
       int element = mat.get_element(i, j); 
       if (!(element < 0 || element > 9)) 
        myfile << " "; 
       myfile << element; 
      } 
     } 
     myfile << ">\n"; 
    } 
    return static_cast<bool>(myfile); 
} 

. 이것은 사용자의 write_one_mat 메소드가 매개 변수를 수정하지 않았기 때문에 의미가 있습니다. mat::get_rows(), mat::get_cols()get_element()const 메서드이므로 mat에서 호출 할 수 있습니다.

+0

변경 사항의 대부분은 차이가 없지만'to_string (element) '만'element'로 바꾸면 다소 느려지 게됩니다. – user3242169

1

이미 주석 처리되었으므로 std::to_string()의 불필요한 사용을 제거하기 시작할 수 있습니다. 스트림은 정수를 기꺼이 직접 포맷 할 수 있습니다. 그러나 정수를 직접 포맷 할 때에도 대부분의 구현에서 dynamic_cast<...>(..)을 사용하는 것으로 보이는 패싯에 대한 불필요한 오버 헤드가 있습니다. (다른 방법을 사용하여 다른 컴파일러에 걸리는 시간을 그래프로하는 this graph를 볼 수있는 가장 빠른 방법을

std::locale loc(std::locale(), new std::num_put<char, char*>()); 
std::num_put<char, char*> const& np(std::use_fast<std::num_put<char, char*>>(loc)); 
char buffer[1024]; 
char* next(buffer); 
for (int i(1); i <= rows; ++i) { 
    for (int j(1); j <= cols; ++j) { 
     int element(mat.get_element(i, j)); 
     if (element < 0 || element < 9) { 
      *next++ = ' '; 
     } 
     next = np.put(next, myfile, ' ', element); 
     if (std::numeric_limits<int>::digits10 + 1 <= (buffer + 1014) - next)) { 
      myfile.write(buffer, next - buffer); 
      next = buffer; 
     } 
    } 
} 
myfile.sputn(buffer, next - buffer); 

std::num_put<...>을 사용하여 직접 같다 : 결과적으로는 같은 것을 사용하여 수동으로 정수를 포맷하는 것이 속도가 더 빠를 수 있습니다 짧은이다 보다 나은).

코드가 공백을 도입하기 위해 이상한 규칙을 사용하여 큰 숫자 시퀀스를 작성하는 것처럼 보입니다. element 이후에 공백을 넣지 않으려하고 각 행 다음에 줄 바꿈을 넣지 않으실 건가요?

+0

실제로이 코드는 원본보다 훨씬 느립니다. 하나. 이런 일이 훨씬 더 빨라질 것으로 기대합니다 ... 어쩌면 문제는 내 고대 컴퓨터와 비주얼 스튜디오에 더 깊숙이 있습니다.) 어쨌든 고마워요! – user3242169

관련 문제