Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "10/07/2011ecs40 fall 20121 Software Development & Object- Oriented Programming ecs40 Fall 2012: Software Development & Object- Oriented Programming #05:"— Presentation transcript:

1 10/07/2011ecs40 fall 20121 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 http://www.cs.ucdavis.edu/~wu/ wu@cs.ucdavis.edu

2 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

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

4 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

5 Built-in UNIX time profiler l Unix C shell 14.918u : 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 [ykhong@vangogh home]$ time./test 14.918u 0.016s 0:15.61 95.5% 0+0k 0+0io 0pf+0w GNU gprof 4/22

6 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 [ykhong@vangogh home]$ time./test real 0m15.456s user 0m14.990s sys 0m0.015s GNU gprof 5/22

7 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

8 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 [ykhong@vangogh home]$ gcc –pg test2.c –o test2 GNU gprof 7/22

9 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 [ykhong@vangogh home]$ gprof –b test2 gmon.out > output.txt GNU gprof 8/22

10 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

11 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

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

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

14 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 71.90 30.17 30.17 1 30.17 30.17 C3 19.42 38.32 8.15 2 4.07 4.07 B2 7.99 41.67 3.35 1 3.35 3.35 C2 0.00 41.67 0.00 1 0.00 37.60 A 0.00 41.67 0.00 1 0.00 33.52 B1 0.00 41.67 0.00 1 0.00 0.00 C1 0.00 41.67 0.00 1 0.00 4.07 D GNU gprof 13/22

15 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

16 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

17 GNU gprof time profiler l Example Program Call graph index %time self children called name [1] 100.0 0.00 41.67 main[1] 0.00 37.60 1/1 A[2] 0.00 4.07 1/1 D[6] ------------------------------------------------------------------ 0.00 37.60 1/1 main[1] [2] 90.2 0.00 37.60 1 A[2] 0.00 33.52 1/1 B1[3] 4.07 0.00 1/2 B2[5] ------------------------------------------------------------------ GNU gprof 16/22

18 GNU gprof time profiler l Example Program 0.00 33.52 1/1 A[2] [3] 80.4 0.00 33.52 1 B1[3] 30.17 0.00 1/1 C3[4] 3.35 0.00 1/1 C2[7] 0.00 0.00 1/1 C1[8] ------------------------------------------------------------------ 30.17 0.00 1/1 B1[3] [4] 72.4 30.17 0.00 1 C3[4] ------------------------------------------------------------------ 4.07 0.00 1/2 A[2] 4.07 0.00 1/2 D[6] [5] 19.6 8.15 0.00 2 B2[5] ------------------------------------------------------------------ GNU gprof 17/22

19 GNU gprof time profiler l Example Program 0.00 4.07 1/1 main[1] [6] 9.8 0.00 4.07 1 D[6] 4.07 0.00 1/2 B2[5] ------------------------------------------------------------------ 3.35 0.00 1/1 B1[3] [7] 8.0 3.35 0.00 1 C2[7] ------------------------------------------------------------------ 0.00 0.00 1/1 B1[3] [8] 0.0 0.00 0.00 1 C1[8] ------------------------------------------------------------------ GNU gprof 18/22

20 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

21 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

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/1 main[1] 1 A[2] 1/1 B1[3] 1/2 B2[5] GNU gprof 21/22

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

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

25 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

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

27 Profiling Tools27 Number of times was invoked Flat profile: Calls

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

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

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

31 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

32 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

33 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

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

35 © 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

36 © 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;

37 © 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

38 © 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

39 © 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];

40 © 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

41 © 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

42 © 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

43 © 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;

44 © 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

45 © 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;

46 © 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

47 © 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

48 © 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

49 © 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

50 © 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;

51 © 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

52 © 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

53 © 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

54 © 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

55 © 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

56 © 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

57 © 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

58 © 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

59 © 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;

60 © 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

61 © 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

62 © 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

63 © 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

64 © 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;

65 © 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)

66 © 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

67 10/07/2011ecs40 fall 201267

68 10/07/2011ecs40 fall 201268

69 10/07/2011ecs40 fall 201269

70 10/07/2011ecs40 fall 201270

71 10/07/2011ecs40 fall 201271

72 10/07/2011ecs40 fall 201272

73 10/07/2011ecs40 fall 201273

74 10/07/2011ecs40 fall 201274

75 10/07/2011ecs40 fall 201275


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

Similar presentations


Ads by Google