๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

์นด์นด์˜ค ์บ์‹œ ๋ฌธ์ œ ๋ณ€ํ˜•(์‚ฝ์ž… ์ •๋ ฌ ์‘์šฉ) ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm (๊ธฐ์ดˆ๋ฌธ์ œํ’€์ด)

์นด์นด์˜ค ์บ์‹œ ๋ฌธ์ œ ๋ณ€ํ˜•(์‚ฝ์ž… ์ •๋ ฌ ์‘์šฉ)

Daehyunii 2022. 9. 6. 19:02

๋ฌธ์ œ(์ถœ์ฒ˜ : ์ธํ”„๋Ÿฐ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด ๊ฐ•์˜, ์ •๋ณด์˜ฌ๋ฆผํ”ผ์•„๋“œ)

์บ์‹œ๋ฉ”๋ชจ๋ฆฌ๋Š” CPU์™€ ์ฃผ๊ธฐ์–ต์žฅ์น˜(DRAM) ์‚ฌ์ด์˜ ๊ณ ์†์˜ ์ž„์‹œ ๋ฉ”๋ชจ๋ฆฌ๋กœ์„œ CPU๊ฐ€ ์ฒ˜๋ฆฌํ•  ์ž‘์—… ์„ ์ €์žฅํ•ด ๋†“์•˜๋‹ค๊ฐ€ ํ•„์š”ํ•  ๋ฐ”๋กœ ์‚ฌ์šฉํ•ด์„œ ์ฒ˜๋ฆฌ์†๋„๋ฅผ ๋†’์ด๋Š” ์žฅ์น˜์ด๋‹ค. ์›Œ๋‚™ ๋น„์‹ธ๊ณ  ์šฉ๋Ÿ‰์ด ์ž‘์•„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ์ฒ ์ˆ˜์˜ ์ปดํ“จํ„ฐ๋Š” ์บ์‹œ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ ๊ทœ์น™์ด LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋”ฐ ๋ฅธ๋‹ค. LRU ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ Least Recently Used ์˜ ์•ฝ์ž๋กœ ์ง์—ญํ•˜์ž๋ฉด ๊ฐ€์žฅ ์ตœ๊ทผ์— ์‚ฌ์šฉ๋˜์ง€ ์•Š ์€ ๊ฒƒ ์ •๋„์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ์บ์‹œ์—์„œ ์ž‘์—…์„ ์ œ๊ฑฐํ•  ๋•Œ ๊ฐ€์žฅ ์˜ค๋žซ๋™์•ˆ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ๊ฒƒ์„ ์ œ๊ฑฐํ•˜๊ฒ ๋‹ค๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜์ž…๋‹ˆ๋‹ค.

๋งŒ์•ฝ์บ์‹œ์˜์‚ฌ์ด์ฆˆ๊ฐ€5์ด๊ณ ์ž‘์—…์ด 2 3 1 6 7 ์ˆœ์œผ๋กœ์ €์žฅ๋˜์–ด์žˆ๋‹ค๋ฉด, (๋งจ ์•ž์ด ๊ฐ€์žฅ ์ตœ๊ทผ์— ์“ฐ์ธ ์ž‘์—…์ด๊ณ , ๋งจ ๋’ค๋Š” ๊ฐ€์žฅ ์˜ค๋žซ๋™์•ˆ ์“ฐ์ด์ง€ ์•Š์€ ์ž‘์—…์ด๋‹ค.)

1) Cache Miss : ํ•ด์•ผํ•  ์ž‘์—…์ด ์บ์‹œ์— ์—†๋Š” ์ƒํƒœ๋กœ ์œ„ ์ƒํƒœ์—์„œ ๋งŒ์•ฝ ์ƒˆ๋กœ์šด ์ž‘์—…์ธ 5๋ฒˆ ์ž‘ ์—…์„ CPU๊ฐ€ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Cache miss๊ฐ€ ๋˜๊ณ  ๋ชจ๋“  ์ž‘์—…์ด ๋’ค๋กœ ๋ฐ€๋ฆฌ๊ณ  5๋ฒˆ์ž‘์—…์€ ์บ์‹œ์˜ ๋งจ

์•ž์—์œ„์น˜ํ•œ๋‹ค. 5 2 3 1 6 (7๋ฒˆ์ž‘์—…์€์บ์‹œ์—์„œ์‚ญ์ œ๋œ๋‹ค.)
2) Cache Hit : ํ•ด์•ผํ•  ์ž‘์—…์ด ์บ์‹œ์— ์žˆ๋Š” ์ƒํƒœ๋กœ ์œ„ ์ƒํƒœ์—์„œ ๋งŒ์•ฝ 3๋ฒˆ ์ž‘์—…์„ CPU๊ฐ€ ์‚ฌ์šฉ

ํ•œ๋‹ค๋ฉด Cache Hit๊ฐ€ ๋˜๊ณ , 63๋ฒˆ ์•ž์— ์žˆ๋Š” 5, 2๋ฒˆ ์ž‘์—…์€ ํ•œ ์นธ ๋’ค๋กœ ๋ฐ€๋ฆฌ๊ณ , 3๋ฒˆ์ด ๋งจ ์•ž์œผ

๋กœ ์œ„์น˜ํ•˜๊ฒŒ ๋œ๋‹ค. 5 2 3 1 6 ---> 3 5 2 1 6

์บ์‹œ์˜ ํฌ๊ธฐ๊ฐ€ ์ฃผ์–ด์ง€๊ณ , ์บ์‹œ๊ฐ€ ๋น„์–ด์žˆ๋Š” ์ƒํƒœ์—์„œ N๊ฐœ์˜ ์ž‘์—…์„ CPU๊ฐ€ ์ฐจ๋ก€๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค๋ฉด N๊ฐœ์˜ ์ž‘์—…์„ ์ฒ˜๋ฆฌํ•œ ํ›„ ์บ์‹œ๋ฉ”๋ชจ๋ฆฌ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์žฅ ์ตœ๊ทผ ์‚ฌ์šฉ๋œ ์ž‘์—…๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์„ธ์š”.

 

โ–ฃ ์ž…๋ ฅ์„ค๋ช…
์ฒซ ๋ฒˆ์งธ ์ค„์— ์บ์‹œ์˜ ํฌ๊ธฐ์ธ S(3<=S<=10)์™€ ์ž‘์—…์˜ ๊ฐœ์ˆ˜ N(5<=N<=1,000)์ด ์ž…๋ ฅ๋œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ค„์— N๊ฐœ์˜ ์ž‘์—…๋ฒˆํ˜ธ๊ฐ€ ์ฒ˜๋ฆฌ์ˆœ์œผ๋กœ ์ฃผ์–ด์ง„๋‹ค. ์ž‘์—…๋ฒˆํ˜ธ๋Š” 1 ~100 ์ด๋‹ค.

โ–ฃ ์ถœ๋ ฅ์„ค๋ช…
๋งˆ์ง€๋ง‰ ์ž‘์—… ํ›„ ์บ์‹œ๋ฉ”๋ชจ๋ฆฌ์˜ ์ƒํƒœ๋ฅผ ๊ฐ€์žฅ ์ตœ๊ทผ ์‚ฌ์šฉ๋œ ์ž‘์—…๋ถ€ํ„ฐ ์ฐจ๋ก€๋กœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

โ–ฃ ์ž…๋ ฅ์˜ˆ์ œ 1

5 9

1 2 3 2 6 2 3 5 7

 

โ–ฃ ์ถœ๋ ฅ์˜ˆ์ œ 1

7 5 3 2 6

 

[์บ์‹œ ๋ฉ”๋ชจ๋ฆฌ ์ƒํƒœ ๋ณ€ํ™”]

10000

21000

32100

23100

62310

26310

32610

53261

75326

 

Tip

 

๋ฌธ์ œํ’€์ด

//๊ฐ•์˜ ๋“ฃ๊ณ  ๋‚ด๊ฐ€ ๋‹ค์‹œ ์ž‘์„ฑํ•œ ๋‹ต
function solution(cacheSize, arr){
    let cacheMemory = Array.from({length : cacheSize}, (v)=>0);

    arr.forEach(element =>{
        let pos = -1;
        for(let i = 0 ; i < cacheSize ; i++){
            if(cacheMemory[i] === element) pos = i;
        }
        if(pos === -1){
            for(let i = cacheMemory.length - 1 ; i >= 1 ; i--){
                cacheMemory[i] = cacheMemory[i-1];
            }
        }else{
            for(let i = pos ; i >= 1 ; i--){
                cacheMemory[i] = cacheMemory[i-1];
            }
        }
        cacheMemory[0] = element;
    })
    return cacheMemory;
}

let arr3=[1, 2, 3, 2, 6, 2, 3, 5, 7, 1];
console.log(solution(5, arr3));