What’s it all about STL vectors offer amortized constant time insertion, by doubling the capacity whenever it’s full Vectors also offer contiguous memory,

Slides:



Advertisements
Similar presentations
M180: Data Structures & Algorithms in Java
Advertisements

ArrayLists David Kauchak cs201 Spring Extendable array Arrays store data in sequential locations in memory Elements are accessed via their index.
CS261 Data Structures Dynamic Arrays. Pro: only core data structure designed to hold a collection of elements Pro: random access: can quickly get to any.
Incremental Algorithms for Dispatching in Dynamically Typed Languages Yoav Zibin Technion—Israel Institute of Technology Joint work with: Yossi (Joseph)
TDDB56 DALGOPT-D TDDB57 DALG-C Lecture 2 Jan Maluszynski - HT TDDB56 – DALGOPT-D Algorithms and optimization TDDB57 – DALG-C Data Structures and.
Standard Containers: Vectors
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 11a. The Vector Class.
Basic C++ Sequential Container Features
Writing Your Own STL Container Ray Lischner
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Data Structures Using C++ 2E
Copyright © 2012 Pearson Education, Inc. Chapter 8 Two Dimensional Arrays.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Containers Overview and Class Vector
CS261 Data Structures Dynamic Arrays Part 2: Implementation.
Natalia Yastrebova What is Coverity? Each developer should answer to some very simple, yet difficult to answer questions: How do I find new.
CS179: GPU Programming Lecture 11: Lab 5 Recitation.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
Chapter 4 Storage Management (Memory Management).
Defining New Types Lecture 21 Hartmut Kaiser
LIBCXXGPU GPU Acceleration for the C++ STL. libcxxgpu  C++ STL provides algorithms over data structures  Thrust provides GPU versions of these algorithms.
Chapter 16 Stacks & Queues. Objective In this chapter we will learn:  Stacks  Queues  Different implementations (arrays and linked list) of both 
CSE 332: C++ STL containers Review: C++ Standard Template Library (STL) The STL is a collection of related software elements –Containers Data structures:
Memory Management Issues, Solutions, and Examples.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ How to Program, 9/e © by Pearson Education, Inc. All Rights Reserved.
Dynamic Array. An Array-Based Implementation - Summary Good things:  Fast, random access of elements  Very memory efficient, very little memory is required.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Advanced Algorithm Design and Analysis (Lecture 12) SW5 fall 2004 Simonas Šaltenis E1-215b
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 17: Vectors and Arrays 1 Based on slides created by Bjarne Stroustrup.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
STL Containers Inside Peter Sikachev Institute of Computer Graphics and Algorithms Vienna University of Technology.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Dynamic Storage Exercise. We saw that by int i; while (std::cin >> i)... we can read inputs as long as there are more available in std::cin. Your task.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
The Container Class A Collection of Data. What is a Container Class?  A class that can contain a collection of items  A list or bag of items of the.
Chapter 17 – Templates. Function Templates u Express general form for a function u Example: template for adding two numbers Lesson 17.1 template Type.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Chapter 8: Memory Management. 8.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts Chapter 8: Memory Management Background Swapping Contiguous.
1 The Standard Template Library The STL is a collection of Container classes These are class templates for containers. A container is an object that stores.
Data Structures and Algorithm Analysis Dr. Ken Cosh Linked Lists.
1 Data Organization Example 1: Heap storage management Maintain a sequence of free chunks of memory Find an appropriate chunk when allocation is requested.
18 Chapter Stacks and Queues
Updating SF-Tree Speaker: Ho Wai Shing.
CSCE 210 Data Structures and Algorithms
Alexandru Razvan Caciulescu University POLITEHNICA of Bucharest
Collections Intro What is the STL? Templates, collections, & iterators
Two Dimensional Arrays
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Object Oriented Programming COP3330 / CGS5409
تصنيف التفاعلات الكيميائية
File Systems Implementation
Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors
Node-based storage with arrays
Main Memory Background Swapping Contiguous Allocation Paging
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
Jordi Cortadella and Jordi Petit Department of Computer Science
Data structures and algorithms
The Standard Template Library
Handling Arrays Completion of ideas needed for a general and complete program Final concepts needed for Final.
COP 3330 Object-oriented Programming in C++
Collections Intro What is the STL? Templates, collections, & iterators
Linked List Intro CSCE 121.
Chapter 3 Lists, Stacks, and Queues
8.3 Vectors Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Dynamic Array: Implementation of Stack
15 – Sequential Containers
Presentation transcript:

What’s it all about STL vectors offer amortized constant time insertion, by doubling the capacity whenever it’s full Vectors also offer contiguous memory, which means that it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size() When the vector needs more memory it allocates a new block and copies the existing data

Ideas to speed up stl::vector Use realloc to expand existing block of memory instead of allocating a new block of memory –Pro: Reduces the need for copying –Pro: realloc handles copying whenever it’s needed –Con: Not all classes are bitwise moveable

What classes are bitwise moveable POD (Plain old datatype) (has no constructor) Classes to which there are no references –References into vectors are not a good idea since insert/deletion might invalidate references –Classes to which there are references, must maintain these references during (copy)construction to function properly within an STL container

How can this be exploited It can’t STL handles all copying of objects by copyconstruction within the containers, effectively disallowing users to optimize this process Allocators offers no solution, since they only deal with memory allocation

Rolling your own vector Creating a new vector that supports both BW-Moveable types and types that need copyconstruction solves the problem, but is not STL compatible In the new vector the allocator takes care of memory allocation and copies objects Creating different allocators, allows us to optimize vector for a given type

Ideas for implementation Type traits –IsPod –IsBWMoveable Enables us to automatically select an allocator at compile-time Three different kinds of allocators –StdAllocator –BWMoveableAllocator –PODAllocator

Benchmarking the new vector LoopsTypeStl::vectorVector 10Int11,605631,907952, Thingie11,292061,858451,85 100Int11,771032,099722, Thingie11,257342,212772, Int11,556672,094712, Thingie10, ,44012, Int11,731162,001342, Thingie10, ,548442, Int11,560261,669131, Thingie10, ,29273,15044

REPS = 10 LOOPS = 10 A: std::vector >::push_back 0, Seconds B: Vector >::push_back 7,93397e-005 Seconds C: Vector >::push_back 6,67683e-005 Seconds D: Vector >::push_back 6,67683e-005 Seconds A/B = 1,60563 A/C = 1,90795 A/D = 2,00881 E: std::vector ::push_back 0, Seconds F: Vector >::push_back 8,8e-005 Seconds G: Vector >::push_back 6,1181e-005 Seconds H: Vector >::push_back 6,14603e-005 Seconds E/F = 1,29206 E/G = 1,85845 E/H = 1,85 REPS = 10 LOOPS = 100 A: std::vector >::push_back 0, Seconds B: Vector >::push_back 0, Seconds C: Vector >::push_back 0, Seconds D: Vector >::push_back 0, Seconds A/B = 1,77103 A/C = 2,09972 A/D = 2,29003 E: std::vector ::push_back 0, Seconds F: Vector >::push_back 0, Seconds G: Vector >::push_back 9,19111e-005 Seconds H: Vector >::push_back 9,07937e-005 Seconds E/F = 1,25734 E/G = 2,21277 E/H = 2,24

REPS = 10 LOOPS = 1000 A: std::vector >::push_back 0, Seconds B: Vector >::push_back 0, Seconds C: Vector >::push_back 0, Seconds D: Vector >::push_back 0, Seconds A/B = 1,55667 A/C = 2,09471 A/D = 2,22324 E: std::vector ::push_back 0, Seconds F: Vector >::push_back 0, Seconds G: Vector >::push_back 0, Seconds H: Vector >::push_back 0, Seconds E/F = 0, E/G = 2,4401 E/H = 2,52282 REPS = 10 LOOPS = A: std::vector >::push_back 0, Seconds B: Vector >::push_back 0, Seconds C: Vector >::push_back 0, Seconds D: Vector >::push_back 0, Seconds A/B = 1,73116 A/C = 2,00134 A/D = 2,06169 E: std::vector ::push_back 0, Seconds F: Vector >::push_back 0, Seconds G: Vector >::push_back 0, Seconds H: Vector >::push_back 0, Seconds E/F = 0, E/G = 2,54844 E/H = 2,85905

REPS = 10 LOOPS = A: std::vector >::push_back 0, Seconds B: Vector >::push_back 0, Seconds C: Vector >::push_back 0, Seconds D: Vector >::push_back 0, Seconds A/B = 1,56026 A/C = 1,66913 A/D = 1,96163 E: std::vector ::push_back 0, Seconds F: Vector >::push_back 0, Seconds G: Vector >::push_back 0, Seconds H: Vector >::push_back 0, Seconds E/F = 0, E/G = 2,2927 E/H = 3,15044