10950번과 입출력은 동일하지만 시간 제한이 1초로 더 짧은 문제다.
//10950
const fs = require("fs");
const input = fs.readFileSync("./dev/stdin").toString().split("\n");
const n = parseInt(input[0]);
for (let i = 0; i < n; i++) {
tem = input[i + 1].split(" ");
console.log(parseInt(tem[0]) + parseInt(tem[1]));
}
따라서 위의 코드를 입력하면 시간 초과가 떠버린다. console.log()로 입력의 갯수만큼 출력을 반복하는 것이 시간 제한에 걸리기 때문이다. 따라서
const fs = require("fs");
const input = fs.readFileSync("./dev/stdin").toString().split("\n");
const n = parseInt(input[0]);
let res = "";
for (let i = 0; i < n; i++) {
tem = input[i + 1].split(" ");
res += parseInt(tem[0]) + parseInt(tem[1]) + "\n";
}
console.log(res);
결과를 하나하나 출력하는 것이 아니라 res 변수에 하나의 문자열로 저장한 후 한 번에 출력한다.
Will console.log reduce JavaScript execution performance?
Will use of the debugging feature console.log reduce JavaScript execution performance? Will it affect the speed of script execution in production environments? Is there an approach to disable con...
stackoverflow.com
대충 예상은 하고 있었지만, console.log()가 프로그램의 성능을 떨어뜨린다는 것을 확실히 알게 됐다. console.log()는 디버깅용으로 쓰이는 함수라서 속도가 느리다고 한다. 뿐만 아니라 서비스 제공 시에 console.log()를 그대로 사용한다면 누구나 디버깅 메시지를 읽을 수 있기 때문에 주의해야 한다고 한다.
728x90
'Algorithm' 카테고리의 다른 글
[2941] 크로아티아 알파벳 - JavaScript (0) | 2022.06.15 |
---|---|
[2775] 부녀회장이 될테야 - JavaScript (0) | 2022.06.15 |
[14681] 사분면 고르기 - JavaScript (Node.js EACCESS 에러 수정) (0) | 2022.06.11 |
JavaScript로 백준 문제 풀기(Node.js 사용) (0) | 2022.06.09 |
[1018] 체스판 다시 칠하기 - Python (0) | 2022.01.26 |