10/07/2011ecs40 fall 20121 Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CHP-5 LinkedList.
GNU gprof Profiler Yu Kai Hong Department of Mathematics National Taiwan University July 19, 2008 GNU gprof 1/22.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2008.
Comparison summary Array based (dynamic) Keeps place for up to 4N elements Each element takes 1 memory places Fast accession time Slow removals and insertion.
1 Chapter 3 Arrays, Linked Lists, and Recursion. 2 Static vs. Dynamic Structures A static data structure has a fixed size This meaning is different than.
© 2006 Pearson Addison-Wesley. All rights reserved5 A-1 Chapter 5 Linked Lists.
1 Data Structures Data Structures Topic #2. 2 Today’s Agenda Data Abstraction –Given what we talked about last time, we need to step through an example.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Marc Smith and Jim Ten Eyck
Linking Objects Together References are particularly important in computer science because they make it possible to represent the relationship among objects.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
Data Structures Using C++ 2E
Review of C++ Programming Part II Sheng-Fang Huang.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
1 Object-Oriented Design Inheritance and Class Examples.
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 10: Trees Data Abstraction & Problem Solving with C++
© 2011 Pearson Addison-Wesley. All rights reserved 11 B-1 Chapter 11 (continued) Trees.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
Application Profiling Using gprof. What is profiling? Allows you to learn:  where your program is spending its time  what functions called what other.
Lists Chapter 6 5/14/15 Adapted from instructor slides Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education,
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 7: Queues Data Abstraction & Problem Solving with C++
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
CS2006- Data Structures I Chapter 5 Linked Lists III.
Chapter 5 Linked Lists. © 2004 Pearson Addison-Wesley. All rights reserved 5 A-2 Preliminaries Options for implementing an ADT –Array Has a fixed size.
Data Abstraction and Problem Solving with C++ Walls and Mirrors, Third Edition, Frank M. Carrano and Janet J. Prichard ©2002 Addison Wesley CHAPTER 4 Linked.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Lists Chapter.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
CC 215 DATA STRUCTURES LINKED LISTS Dr. Manal Helal - Fall 2014 Lecture 3 AASTMT Engineering and Technology College 1.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
Profiling with GNU GProf
Linked List ADT used to store information in a list
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 6 Section 6.4 – 6.6
Chapter 5 Linked Lists © 2011 Pearson Addison-Wesley. All rights reserved.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 Linked Lists.
Chapter 4 Linked Lists
Chapter 4 Link Based Implementations
Chapter 4 Linked Lists.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Figure 4.1 a) A linked list of integers; b) insertion; c) deletion.
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Linked Lists Chapter 5 (continued)
Presentation transcript:

10/07/2011ecs40 fall Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05: gprof and Chapters 20 – Linked List Dr. S. Felix Wu Computer Science Department University of California, Davis

Profiling Tools2 The Software Optimization Process Find hotspots Modify application Retest using benchmark Investigate causes Create benchmark Hotspots are areas in your code that take a long time to execute

Two types of time profilers l Built-in UNIX time profiler - Unix C shell - Others l GNU gprof time profiler GNU gprof 2/22

Built-in UNIX time profiler l The simplest one. l Does not increase extra time cost. l Does not provide subroutine relative graph. l Does not have to recompile source code. GNU gprof 3/22

Built-in UNIX time profiler l Unix C shell u : elapsed user time 0.016s : elapsed system time 0:15.61 : total elapsed time 95.5% : percent of total CPU usage 0+0k : shared/non-shared memory 0+0io : times for performing input/output home]$ time./test u 0.016s 0: % 0+0k 0+0io 0pf+0w GNU gprof 4/22

Built-in UNIX time profiler l Others real 0m15.456s : total elapsed time user 0m14.990s : user elapsed time sys 0m0.015s : system elapsed time home]$ time./test real 0m15.456s user 0m14.990s sys 0m0.015s GNU gprof 5/22

GNU gprof time profiler l Detail time statistics for each subroutine. l Create relative graph for all subroutines. l Analysis the program bottleneck. l Increase about 30% extra time cost. GNU gprof 6/22

GNU gprof time profiler l Recompile the original source code -pg : This option affects both compiling and linking. Add additional commands into source code when compiling code in order to trace all subroutines. Add essential initial settings and statistical processes when linking the objects. gcc –pg SourceCode –o ExecutableFile home]$ gcc –pg test2.c –o test2 GNU gprof 7/22

GNU gprof time profiler l Convert produced profile data into text file - ListOfOptions can be omitted. - ExecuteFile can be omitted when the file name is a.out. - StatFiles can be omitted when the file name is gmon.out. gprof ListOfOptions ExecuteFile StatFiles > OutputFile home]$ gprof –b test2 gmon.out > output.txt GNU gprof 8/22

GNU gprof time profiler l List of Options -b : omit the table or data illustration on OutputFile. -e(E) SRName : exclude the subroutine SRName from the table (and exclude its elapsed time). -f(F) SRName : only display the subroutine SRName on the table (and its elapsed time). GNU gprof 9/22

GNU gprof time profiler l List of Options -s : combine more than one StatFile into single one with default file name gmon.sum. -Z : only display all subroutines table which are unused on the program. GNU gprof 10/22

GNU gprof time profiler l Example Program subroutine relative graph GNU gprof 11/22

GNU gprof time profiler l Example Program home]$ gcc –pg test.c –o test home]$./test home]$ gprof –b test gmon.out > output home]$ more output GNU gprof 2/22

GNU gprof time profiler l Example Program Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls s/call s/call name C B C A B C D GNU gprof 13/22

GNU gprof time profiler l Example Program % time : the percent of self seconds from total program elapsed time. cumulative seconds : the seconds cumulate from self seconds. self seconds : total elapsed time called by its parents, not including its children’s elapsed time. equal to (self s/call)*(calls) GNU gprof 14/22

GNU gprof time profiler l Example Program calls : total number for each subroutine called by its parents. self s/call : elapsed time for each time called by its parents, not including its children’s elapsed time. total s/call : total elapsed time called by its parents, including its children’s elapsed time. name : subroutine name. GNU gprof 15/22

GNU gprof time profiler l Example Program Call graph index %time self children called name [1] main[1] /1 A[2] /1 D[6] /1 main[1] [2] A[2] /1 B1[3] /2 B2[5] GNU gprof 16/22

GNU gprof time profiler l Example Program /1 A[2] [3] B1[3] /1 C3[4] /1 C2[7] /1 C1[8] /1 B1[3] [4] C3[4] /2 A[2] /2 D[6] [5] B2[5] GNU gprof 17/22

GNU gprof time profiler l Example Program /1 main[1] [6] D[6] /2 B2[5] /1 B1[3] [7] C2[7] /1 B1[3] [8] C1[8] GNU gprof 18/22

l Example Program [ ] index value for each subroutine GNU gprof time profiler [2]A [8]C1 [6]D [3]B1 [7]C2 [5]B2 [4]C3 GNU gprof 19/22

l Example Program See the second part. A on the left-most means the relation is viewed for A. B1 and B2 are below of A means A is the parent of B1 and B2 (A calls B1 and B2), main is above of A means A is the child of main (A called by main). GNU gprof time profiler name main[1] A[2] D[6] main[1] A[2] B1[3] B2[5] GNU gprof 20/22

l Example Program See the second part. 1/2 means subroutine B2 is called by its parents total two times and A calls it only one time. 1 means subroutine A have only one parent. GNU gprof time profiler called name main[1] 1/1 A[2] 1/1 D[6] /1 main[1] 1 A[2] 1/1 B1[3] 1/2 B2[5] GNU gprof 21/22

l Example Program See the second part It is viewed for subroutine A. The elapsed time of A’s children is seconds and B1’s children is seconds. Especially, B2’s children is 0 second since B2 doesn’t have any child. GNU gprof time profiler children name main[1] A[2] 4.07 D[6] main[1] A[2] B1[3] 0.00 B2[5] GNU gprof 22/22

Profiling Tools24 Flat profile : %time Percentage of the total execution time your program spent in this function. These should all add up to 100%.

Profiling Tools25 Flat profile: Cumulative seconds This is cumulative total number of seconds the spent in this functions, plus the time spent in all the functions above this one

Profiling Tools26 Number of seconds accounted for this function alone Flat profile: Self seconds

Profiling Tools27 Number of times was invoked Flat profile: Calls

Profiling Tools28 Average number of sec per call Spent in this function alone Flat profile: Self seconds per call

Profiling Tools29 Average number of seconds spent in this function and its descendents per call Flat profile: Total seconds per call

Profiling Tools30 Call Graph : call tree of the program Current Function: g( ) Called by : main ( ) Descendants: doit ( )

Profiling Tools31 Call Graph : understanding each line Current Function: g( ) Unique index of this function Percentage of the `total ‘ time spent in this function and its children. Total time propagated into this function by its children total amount of time spent in this function Number of times was called

Profiling Tools32 Call Graph : understanding each line Current Function: g( ) Time that was propagated from the function's children into this parent Time that was propagated directly from the function into this parent Number of times this parent called the function `/ ‘ total number of times the function was called Call Graph : parents numbers

Profiling Tools33 Call Graph : “ children ” numbers Current Function: g( ) Amount of time that was propagated from the child's children to the function Amount of time that was propagated directly from the child into function Number of times this function called the child `/ ‘ total number of times this child was called

Linked List 10/07/2011ecs40 fall A B C

© 2005 Pearson Addison-Wesley. All rights reserved4-35Pointers The expression, *p, denotes the memory cell to which p points l The & address-of operator places the address of a variable into a pointer variable –p = &x; Figure 4.2 A pointer to an integer

© 2005 Pearson Addison-Wesley. All rights reserved4-36 Pointers The delete operator returns dynamically allocated memory to the system for reuse, and leaves the variable undefined –delete p ; –A pointer to a deallocated memory cell is possible and dangerous Assign the pointer q the value in p q = p;

© 2005 Pearson Addison-Wesley. All rights reserved4-37Pointers Figure 4.3 (a) declaring pointer variables; (b) pointing to statically allocating memory; (c) assigning a value; (d) allocating memory dynamically; (e) assigning a value

© 2005 Pearson Addison-Wesley. All rights reserved4-38Pointers Figure 4.3 (f) copying a pointer; (g) allocating memory dynamically and assigning a value; (h) assigning NULL to a pointer variable; (i) deallocating memory

© 2005 Pearson Addison-Wesley. All rights reserved4-39 Dynamic Allocation of Arrays Use the new operator to allocate an array dynamically An array name is a pointer to the array ’ s first element l The size of a dynamically allocated array can be increased double* oldArray = anArray; anArray = new double[2*arraySize];

© 2005 Pearson Addison-Wesley. All rights reserved4-40 Pointer-Based Linked Lists A node in a linked list is usually a struct struct Node { int item Node *next; }; //end struct l A node is dynamically allocated Node *p; p = new Node; Figure 4.6 A node

© 2005 Pearson Addison-Wesley. All rights reserved4-41 Pointer-Based Linked Lists l The head pointer points to the first node in a linked list If head is NULL, the linked list is empty Executing the statement the statement head=new Node before head=NULL will result in a lost cell

© 2005 Pearson Addison-Wesley. All rights reserved4-42 Pointer-Based Linked Lists Figure 4.7 A head pointer to a list Figure 4.8 A lost cell

© 2005 Pearson Addison-Wesley. All rights reserved4-43 Displaying the Contents of a Linked List l Reference a node member with the -> operator p->item; l A traverse operation visits each node in the linked list –A pointer variable cur keeps track of the current node for (Node *cur = head; cur != NULL; cur = cur->next) cout item << endl;

© 2005 Pearson Addison-Wesley. All rights reserved4-44 Displaying the Contents of a Linked List Figure 4.9 The effect of the assignment cur = cur->next

© 2005 Pearson Addison-Wesley. All rights reserved4-45 Deleting a Specified Node from a Linked List l Deleting an interior node prev->next=cur->next; l Deleting the first node head=head->next; l Return deleted node to system cur->next = NULL; delete cur; cur=NULL;

© 2005 Pearson Addison-Wesley. All rights reserved4-46 Deleting a Specified Node from a Linked List Figure 4.10 Deleting a node from a linked list Figure 4.11 Deleting the first node

© 2005 Pearson Addison-Wesley. All rights reserved4-47 Inserting a Node into a Specified Position of a Linked List l To insert a node between two nodes newPtr->next = cur; prev->next = newPtr; Figure 4.12 Inserting a new node into a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-48 Inserting a Node into a Specified Position of a Linked List l To insert a node at the beginning of a linked list newPtr->next = head; head = newPtr; Figure 4.13 Inserting at the beginning of a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-49 Inserting a Node into a Specified Position of a Linked List Inserting at the end of a linked list is not a special case if cur is NULL newPtr->next = cur; prev->next = newPtr; Figure 4.14 Inserting at the end of a linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-50 Inserting a Node into a Specified Position of a Linked List l Determining the point of insertion or deletion for a sorted linked list of objects for(prev = NULL, cur= head; (cur != null)&& (newValue > cur->item); prev = cur, cur = cur->next;

© 2005 Pearson Addison-Wesley. All rights reserved4-51 A Pointer-Based Implementation of the ADT List l Public methods –isEmpty –getLength –insert –remove –retrieve l Private method –find l Private Data Members –head –Size l Local variables to member functions –cur –prev

© 2005 Pearson Addison-Wesley. All rights reserved4-52 Constructors and Destructors l Default constructor initializes size and head l Copy constructor allows a deep copy –Copies the array of list items and the number of items l A destructor is required for dynamically allocated memory

© 2005 Pearson Addison-Wesley. All rights reserved4-53 Comparing Array-Based and Pointer-Based Implementations l Size –Increasing the size of a resizable array can waste storage and time l Storage requirements –Array-based implementations require less memory than a pointer-based ones

© 2005 Pearson Addison-Wesley. All rights reserved4-54 Comparing Array-Based and Pointer-Based Implementations l Access time –Array-based: constant access time –Pointer-based: the time to access the i th node depends on i l Insertion and deletions –Array-based: require shifting of data –Pointer-based: require a list traversal

© 2005 Pearson Addison-Wesley. All rights reserved4-55 Saving and Restoring a Linked List by Using a File l Use an external file to preserve the list between runs l Do not write pointers to a file, only data l Recreate the list from the file by placing each item at the end of the list –Use a tail pointer to facilitate adding nodes to the end of the list –Treat the first insertion as a special case by setting the tail to head

© 2005 Pearson Addison-Wesley. All rights reserved4-56 Passing a Linked List to a Function A function with access to a linked list ’ s head pointer has access to the entire list l Pass the head pointer to a function as a reference argument Figure 4.22 A head pointer as a value argument

© 2005 Pearson Addison-Wesley. All rights reserved4-57 Processing Linked Lists Recursively l Recursive strategy to display a list –Write the first node of the list –Write the list minus its first node l Recursive strategies to display a list backward –writeListBackward strategy l Write the last node of the list l Write the list minus its last node backward

© 2005 Pearson Addison-Wesley. All rights reserved4-58 Processing Linked Lists Recursively –writeListBackward2 strategy l Write the list minus its first node backward l Write the first node of the list l Recursive view of a sorted linked list –The linked list to which head points is a sorted list if head is NULL or head->next is NULL or head->item next->item, and head->next points to a sorted linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-59 Objects as Linked List Data l Data in a linked list node can be an instance of a class typedef ClassName ItemType; struct Node { ItemType item; Node *next; }; //end struct Node *head;

© 2005 Pearson Addison-Wesley. All rights reserved4-60 Circular Linked Lists l Last node references the first node l Every node has a successor l No node in a circular linked list contains NULL Figure 4.25 A circular linked list

© 2005 Pearson Addison-Wesley. All rights reserved4-61 Dummy Head Nodes l Dummy head node –Always present, even when the linked list is empty –Insertion and deletion algorithms initialize prev to reference the dummy head node, rather than NULL Figure 4.27 A dummy head node

© 2005 Pearson Addison-Wesley. All rights reserved4-62 Doubly Linked Lists l Each node points to both its predecessor and its successor l Circular doubly linked list –precede pointer of the dummy head node points to the last node –next reference of the last node points to the dummy head node –No special cases for insertions and deletions

© 2005 Pearson Addison-Wesley. All rights reserved4-63 Doubly Linked Lists Figure 4.29 (a) A circular doubly linked list with a dummy head node (b) An empty list with a dummy head node

© 2005 Pearson Addison-Wesley. All rights reserved4-64 Doubly Linked Lists To delete the node to which cur points (cur->precede)->next = cur->next; (cur->next)->precede = cur->precede; To insert a new node pointed to by newPtr before the node pointed to by cur newPtr->next = cur; newPtr->precede = cur->precede; cur->precede = newPtr; newPtr->precede->next = newPtr;

© 2005 Pearson Addison-Wesley. All rights reserved4-65 Application: Maintaining an Inventory l Operations on the inventory –List the inventory in alphabetical order by title (L command) –Find the inventory item associated with title (I, M, D, O, and S commands) –Replace the inventory item associated with a title (M, D, R, and S commands) –Insert new inventory items (A and D commands)

© 2005 Pearson Addison-Wesley. All rights reserved4-66 The C++ Standard Template Library The STL contains class templates for some common ADTs, including the list class l The STL provides support for predefined ADTs through three basic items –Containers are objects that hold other objects –Algorithms act on containers –Iterators provide a way to cycle through the contents of a container

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall

10/07/2011ecs40 fall