forked from gyoogle/tech-interview-for-developer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gyusuk
committed
Jul 31, 2019
1 parent
06bcb3d
commit d47a4c8
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
## [알고리즘] 간단하지만 알면 좋은 최적화들 | ||
|
||
**1. for문의 ++i와 i++ 차이** | ||
|
||
``` | ||
for(int i = 0; i < 1000; i++) { ... } | ||
for(int i = 0; i < 1000; ++i) { ... } | ||
``` | ||
|
||
내부 operator 로직을 보면 i++은 한번더 연산을 거친다. | ||
|
||
따라서 ++i가 미세하게 조금더 빠르다. | ||
|
||
하지만 요즘 컴파일러는 거의 차이가 없어지게 되었다고 한다. | ||
|
||
|
||
|
||
**2. if/else if vs switch case** | ||
|
||
> '20개의 가지 수, 10억번의 연산이 진행되면?' | ||
if/else 활용 : 약 20초 | ||
|
||
switch case : 약 15초 | ||
|
||
|
||
|
||
**switch case**가 더 빠르다. (경우를 찾아서 접근하기 때문에 더 빠르다) | ||
|
||
if-else 같은 경우는 다 타고 들어가야하기 때문에 더 느리다. | ||
|
||
|
||
|
||
**3. for문 안에서 변수 선언 vs for문 밖에서 변수 선언** | ||
|
||
임시 변수의 선언 위치에 따른 비교다. | ||
|
||
for문 밖에서 변수를 선언하는 것이 더 빠르다. | ||
|
||
|
||
|
||
|
||
|
||
**4. 재귀함수 파라미터를 전역으로 선언한 것 vs 재귀함수를 모두 파라미터로 넘겨준 것** | ||
|
||
> '10억번의 연산을 했을 때?' | ||
전역으로 선언 : 약 6.8초 | ||
|
||
파라미터로 넘겨준 것 : 약 9.6초 | ||
|
||
|
||
|
||
함수를 계속해서 호출할 때, 스택에서 쌓인다. 파라미터들은 함수를 호출할 때마다 메모리 할당하는 동작을 반복하게 된다. 따라서 지역 변수로 사용하지 않는 것들은 전역 변수로 빼야한다. |