나의 발자취

백준 2741 본문

computer language/C++

백준 2741

달모드 2020. 9. 14. 01:56
#include<iostream>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i=1; i<=n; i++){
        cout << i << "\n";
    }
    return 0;
}

/*
std::cin 은 scanf보다 3배 가까이 느리다.
이유는 cpp의 iostream의 버퍼와 c의 스튜디오 버퍼와 동기화시켜주므로 두개의 버퍼사용효과.
하지만 std::ios_base::sync_with_stdio(false);를 사용하며 동기화를 끊으면
c++만의 독립적인 버퍼를 생성하게 되고 c의 버퍼들과는 병행하여 사용할 수 없게 되며 속도 높아짐.
이때 std::cout << std::endl; 같이 사용하는 경우 20배 이상 속도차이가 난다.

*/

12ms...

'computer language > C++' 카테고리의 다른 글

[10818] 최소, 최대  (0) 2020.11.18
8393. 팩토리얼 계산 (풀이 두개, 메모리 비교)  (0) 2020.10.13
백준 1330. 시계  (0) 2020.10.07
백준 1008 - A/B 출력  (0) 2020.10.07
백준 11021번  (0) 2020.09.14
Comments