SlideShare ist ein Scribd-Unternehmen logo
1 von 14
1. Vector를 생성하거나 복사할
때 미리 크기를 지정한다.
(크기를 아는 경우)
1. Vector를 생성하거나 복사할 때 미리 크기를 지정한다.
• Vector의 memory가 다 차면 더 크게 재할
당을 받는다.
• 메모리 재 할당
1. vector_old보다 큰 새로운 vector_new
memory 할당
2. Vector_old의 내용을 vector_new에 복사
3. Vector_old의 내용 삭제
4. Vector_old의 메모리 할당 해제
1. Vector를 생성하거나 복사할 때 미리 크기를 지정한다.
• 만약 그냥 vector를 생성해서 값을 하나하
나 넣어준다면?
• 새롭게 값을 넣어주다가 계속해서 메모리
재 할당이 발생
• 크기를 아는 경우 한번에 크게 할당 해 놓
으면 메모리 재 할당이 일어나지 않는다.
2. vector.shrink_to_fit()
• Vector.clear()와
• Vector.erase()는 vector의 내용을 지워주는
함수지만 할당된 메모리는 해제 x
• Vector.shrink_to_fit()은 vector의 메모리를
vector.size()에 맞춰서 재 할당한다.
2. vector.shrink_to_fit()
• Vector.shrink_to_fit()을 지원하지 않을 때
• Vector<T>().swap(vector_cur)
– 텅 빈 벡터와 현재 벡터를 교환해준다.
– 벡터의 모든 값을 삭제하고 메모리도 초기화
• Vector<T>(vector_cur).swap(vector_cur)
– 자기 자신을 넣은 복사 생성자와 교환해준다.
– 현재 size만큼만 메모리가 남게 된다.
3. 대입연산자 vs insert vs
push_back
• 대입연산자
– 기존 벡터의 크기를 알고 있다.
– 새로운 벡터의 크기 재 할당은 한번만.
– vector가 아닌 다른 컨테이너에서 복사하는
경우에는 대입연산자는 사용할 수 없다.
• 이 때는 insert를 사용하자.
3. 대입연산자 vs insert vs
push_back
• Vector.Insert(p,b,e)
– Vector의 p 위치에 반복자 b부터 e까지의 값
을 넣어준다.
– 이 경우도 반복자의 시작과 끝을 알고 있기 때
문에 재 할당이 반복해서 일어나지 않는다.
3. 대입연산자 vs insert vs
push_back
• Push_back()
– 공간이 부족해질 때마다 재 할당이 발생
– 비효율적
– 복사 할 크기를 알고 있다면 미리 메모리 할
당부터 하고 값을 복사 해 오면 좀 나아지지
않을까…
4. 반복자 vs vector.at() vs vector[i]
• 이 경우 at()이 속도가 가장 느리다.
• 왜?
• at(p) 함수의 경우 p의 값을 vector.size()
와 비교해서 적절한 위치에 접근하는지를
판단한 이후 값을 반환해준다.
• 따라서 안정성은 높다.
4. 반복자 vs vector.at() vs vector[i]
• Vector[i] 의 경우 속도는 빠르지만 잘못된
위치에 접근할 수 있기 때문에 치명적인
오류를 발생시킬 수 있다.
5. Vector.insert(p,x)
– vector의 p 위치에 x 값을 입력한다.
– p위치 이후로 있는 원소들이 모두 한 칸씩 뒤
로 밀려야 한다. -> 매우 비효율적
– 만약 반복적으로 insert(p,x) 를 사용해야 하는
경우가 생긴다면?
• 구조를 다시 짜보자..
6. push_back() vs emplace_back()
• Vector<T> vector_test;
• Vector_test.push_back( T );
• Vector_test.emplace_back( T생성 인자)
6. push_back() vs emplace_back()
• Push_back()
– 객체를 전달한다.
– 전달받은 객체를 vector의 맨 뒤에 넣어준다.
– 새로운 값을 입력하는 경우에는 임시 객체의
생성과 이동생성자의 호출, 임시 객체의 파괴
가 일어난다.
6. push_back() vs emplace_back()
• Emplace_back
– 객체생성을 위한 인자를 전달한다.
– 전달받은 인자로 생성한 객체를 vector의 맨
뒤에 넣어준다.
– 전달받은 인자로 생성한 객체가 vector에 바
로 저장되기 때문에 임시 객체가 생성되지 않
는다.

Weitere ähnliche Inhalte

Empfohlen

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Empfohlen (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

6 tips to supercharge c++11 vector performance

  • 1. 1. Vector를 생성하거나 복사할 때 미리 크기를 지정한다. (크기를 아는 경우)
  • 2. 1. Vector를 생성하거나 복사할 때 미리 크기를 지정한다. • Vector의 memory가 다 차면 더 크게 재할 당을 받는다. • 메모리 재 할당 1. vector_old보다 큰 새로운 vector_new memory 할당 2. Vector_old의 내용을 vector_new에 복사 3. Vector_old의 내용 삭제 4. Vector_old의 메모리 할당 해제
  • 3. 1. Vector를 생성하거나 복사할 때 미리 크기를 지정한다. • 만약 그냥 vector를 생성해서 값을 하나하 나 넣어준다면? • 새롭게 값을 넣어주다가 계속해서 메모리 재 할당이 발생 • 크기를 아는 경우 한번에 크게 할당 해 놓 으면 메모리 재 할당이 일어나지 않는다.
  • 4. 2. vector.shrink_to_fit() • Vector.clear()와 • Vector.erase()는 vector의 내용을 지워주는 함수지만 할당된 메모리는 해제 x • Vector.shrink_to_fit()은 vector의 메모리를 vector.size()에 맞춰서 재 할당한다.
  • 5. 2. vector.shrink_to_fit() • Vector.shrink_to_fit()을 지원하지 않을 때 • Vector<T>().swap(vector_cur) – 텅 빈 벡터와 현재 벡터를 교환해준다. – 벡터의 모든 값을 삭제하고 메모리도 초기화 • Vector<T>(vector_cur).swap(vector_cur) – 자기 자신을 넣은 복사 생성자와 교환해준다. – 현재 size만큼만 메모리가 남게 된다.
  • 6. 3. 대입연산자 vs insert vs push_back • 대입연산자 – 기존 벡터의 크기를 알고 있다. – 새로운 벡터의 크기 재 할당은 한번만. – vector가 아닌 다른 컨테이너에서 복사하는 경우에는 대입연산자는 사용할 수 없다. • 이 때는 insert를 사용하자.
  • 7. 3. 대입연산자 vs insert vs push_back • Vector.Insert(p,b,e) – Vector의 p 위치에 반복자 b부터 e까지의 값 을 넣어준다. – 이 경우도 반복자의 시작과 끝을 알고 있기 때 문에 재 할당이 반복해서 일어나지 않는다.
  • 8. 3. 대입연산자 vs insert vs push_back • Push_back() – 공간이 부족해질 때마다 재 할당이 발생 – 비효율적 – 복사 할 크기를 알고 있다면 미리 메모리 할 당부터 하고 값을 복사 해 오면 좀 나아지지 않을까…
  • 9. 4. 반복자 vs vector.at() vs vector[i] • 이 경우 at()이 속도가 가장 느리다. • 왜? • at(p) 함수의 경우 p의 값을 vector.size() 와 비교해서 적절한 위치에 접근하는지를 판단한 이후 값을 반환해준다. • 따라서 안정성은 높다.
  • 10. 4. 반복자 vs vector.at() vs vector[i] • Vector[i] 의 경우 속도는 빠르지만 잘못된 위치에 접근할 수 있기 때문에 치명적인 오류를 발생시킬 수 있다.
  • 11. 5. Vector.insert(p,x) – vector의 p 위치에 x 값을 입력한다. – p위치 이후로 있는 원소들이 모두 한 칸씩 뒤 로 밀려야 한다. -> 매우 비효율적 – 만약 반복적으로 insert(p,x) 를 사용해야 하는 경우가 생긴다면? • 구조를 다시 짜보자..
  • 12. 6. push_back() vs emplace_back() • Vector<T> vector_test; • Vector_test.push_back( T ); • Vector_test.emplace_back( T생성 인자)
  • 13. 6. push_back() vs emplace_back() • Push_back() – 객체를 전달한다. – 전달받은 객체를 vector의 맨 뒤에 넣어준다. – 새로운 값을 입력하는 경우에는 임시 객체의 생성과 이동생성자의 호출, 임시 객체의 파괴 가 일어난다.
  • 14. 6. push_back() vs emplace_back() • Emplace_back – 객체생성을 위한 인자를 전달한다. – 전달받은 인자로 생성한 객체를 vector의 맨 뒤에 넣어준다. – 전달받은 인자로 생성한 객체가 vector에 바 로 저장되기 때문에 임시 객체가 생성되지 않 는다.