Download presentation
Presentation is loading. Please wait.
1
Functionality & Performance in Milestone 1
2
Streets Database Library
Milestone 1 at a Glance Main Executable Streets Database Library Street Map Library Tester Executable
3
Streets Database Library
Milestone 1 at a Glance Cannot Modify! Main Executable Streets Database Library Street Map Library Tester Executable
4
Streets Database Library
Milestone 1 at a Glance Already Implemented Cannot Modify! Main Executable To be implemented load_map close_map Streets Database Library Street Map Library Tester Executable load_map close_map
5
Streets Database Library
Milestone 1 at a Glance Already Implemented Cannot Modify! Main Executable To be implemented load_map close_map Streets Database Library Street Map Library Tester Executable load_map Load, close, queries, etc. queries close_map
6
What you will need… To read the handout
Seriously… At least one global variable (to pass performance tests) Why? Containers from the STL std::vector, std::list std::map, std::unordered_map, std::multimap Which containers should we choose?
7
What you are given…
8
Recommended Steps for m1
Compile Run ece297exercise 1
9
Recommended Steps for m1
Compile Run ece297exercise 1 /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:185: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:190: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:195: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:200: undefined reference to `find_intersection_street_names(unsigned int)' /cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:205: undefined reference to `find_intersection_street_names(unsigned int)' /tmp/ccVscFJr.o:/cad2/ece297s/public//m1/tests/m1_func_intersection_toronto_canada_public.cpp:210: more undefined references to `find_intersection_street_names(unsigned int)' follow collect2: error: ld returned 1 exit status Build Error: Failed to build unit tests. Compiler returned non-zero exit status 1
10
Recommended Steps for m1
Compile Run ece297exercise 1 Create “empty” implementations of functions in m1.h Make sure you return something if the function requires it No more build errors with ece297exercise
11
Recommended Steps for m1
Compile Run ece297exercise 1 Create “empty” implementations of functions in m1.h Fail all the tests Your goal for m1: Fail 0 of the tests
12
Breaking Maps Up Into Parts
13
Breaking Maps Up Into Parts
Intersection Street Segment
14
Find Intersection Street Segments
Return a vector of all the street segments connected to an intersection Our only input is an unsigned number Need to use something that is outside the scope of the function StreetsDatabaseAPI.h has what we need
15
Street Segments in StreetDatabaseAPI.h
16
Other Functions in StreetDatabaseAPI.h
17
Understanding the Functions
Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 Street Segment ID: 352
18
Understanding the Functions
Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352
19
Understanding the Functions
getIntersectionStreetSegment(232, {0, 1, 2}); will return {311, 321, 352} Which street segment IDs correspond to 0, 1, or 2 is irrelevant. StreetsDatabaseAPI will ensure it is consistent for a given map Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352
20
Understanding the Functions
getIntersectionStreetSegment(232, {0, 1, 2}); will return one of {311, 321, 352} Which street segment IDs correspond to 0, 1, or 2 is irrelevant. StreetsDatabaseAPI will ensure it is consistent for a given map Street Segment ID: 311 Intersection ID: 232 Street Segment ID: 321 getIntersectionStreetSegmentCount(232); will return 3 Street Segment ID: 352 getStreetSegmentInfo({311, 321, 352}) will get the struct we saw earlier containing additional information for the street segment ID
21
Other Functions in StreetDatabaseAPI.h
Be very careful: IntersectionIndex, StreeSegmentIndex, etc. are all typedefs to unsigned. If you illogically compare an intersection ID with a street segment ID, you will not get a compile error. Use appropriate variable names to avoid this issue.
22
Implement the Function
You will likely need one or more of these function calls: getIntersectionStreetSegmentCount(…) getIntersectionStreetSegment(…) getStreetSegmentInfo(…) Write your implementation here. Add street segment IDs to the ss_ids vector.
23
Run only the functionality tests for intersections:
Test The Function Run only the functionality tests for intersections: Only 3 of 4 failed, which means we passed the functionality test for find_intersection_street_segments.
24
Complexity of Our Function
Assume these are O(1): getIntersectionStreetSegmentCount( …) getIntersectionStreetSegment(…) getStreetSegmentInfo(…) Complexity: O(n) What is n? Can we reduce the complexity of our function?
25
Massive hint in m1.cpp
26
Massive hint in m1.cpp What data structure can we use for intersection names?
27
What are the inputs/outputs of our function?
intersection_id Type: unsigned Output std::vector<unsigned>
28
Viewing STL data-structures as input/output
STL Container Input Output Example vector unsigned integer Templated vector<MyObject> objects; … unsigned input = 5; MyObject output = objects[input]; list (similar to vector) map Templated Key Templated Value map<unsigned, MyObject> objects; unsigned input = 3; MyObject output = objects[input] unordered_map (similar to map)
29
Potential Data Structures
Which data structures can be indexed with an unsigned intersection_id? What can these data structures contain? What is the complexity of accessing elements in the data structure?
30
Potential Data Structures
Which data structures can be indexed with an unsigned intersection_id? std::vector, std::list, std::map, std::unordered_map What can these data structures contain? Anything, they are templated What is the complexity of accessing elements in the data structure? std::vector – O(1), std::list – O(n) std::map – O(log n), std::unordered_map – O(1) We would want something less complex than O(n)
31
Complexity versus Performance
std::vector std::unordered_map Accessing an element takes O(1) Accessing an element takes O(1)
32
Complexity versus Performance
std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1)
33
Complexity versus Performance
std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1) An access involves: A call to a hash function to get an index A direct call to memory
34
Complexity versus Performance
std::vector std::unordered_map Accessing an element takes O(1) An access involves a direct call to memory (a “load” instruction – ECE243) Accessing an element takes O(1) An access involves: A call to a hash function to get an index A direct call to memory The hash function takes time!
35
When do I need a hash function?
Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? What does unordered_map’s hash function require?
36
When do I need a hash function?
Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? std::string Custom class What does unordered_map’s hash function require?
37
When do I need a hash function?
Arrays, lists, and vectors are indexed using a number If we are not indexing with a number, we should use a hash function The first element starts at 0 When do I need to not index via a number? std::string Custom class What does unordered_map’s hash function require? An operator== for the data type being used
38
Loading Data First Notice the global variable.
Populate intersection_street_segments with the street segment IDs for each intersection.
39
Loading Data First – Option 1
First we loop through all intersections. Remember to use a useful variable name!
40
Loading Data First – Option 1
Then we add an empty “nested” vector into our global variable. This empty vector will contain the street segment IDs, which is the output of the function we were implementing.
41
Loading Data First – Option 1
Now, just like before, we loop through the number of street segments connected to the intersection.
42
Loading Data First – Option 1
… and we add the street segment IDs to our “nested” vector, which we can access using the operator[] of the global variable.
43
Loading Data First – Option 2
In Option 2, we remove the need to push back empty vectors into our global variable. Instead, we use vector’s “resize” function. This will change how many elements the vector holds, and allows us to use operator[] with a value of up to the number of intersections.
44
How does this impact the find function?
Since the output of our global variable is a vector of street segment IDs, we can just index into it using the intersection ID. Our function is now one line long, but faster since it only has to do an array look up.
45
How does this impact performance?
We moved the complexity to load_map What is the complexity of the load_map function? What is the complexity of find_intersection_street_segments? There is a performance test for load_map too! You have three seconds to load everything into your data structures
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.