Code Analysis: CSE 373 Data Structures and Algorithms
Code Analysis: CSE 373 Data Structures and Algorithms
Software Test: a separate piece of code that exercises the code you are assessing by providing
input to your code and finishes with an assertion of what the result should be.
Trust no one!
- How can the user mess up?
For large enough the dominant term decides how big the function is.
The Java bytecode is converted (compiled) into your own machine’s assembly code
- Might change the number of lines again.
The number of lines still isn’t a perfect reflection of time taken by your laptop.
The amount of time it takes to look up a value in memory is wildly variable
Recently used values are probably “cached” and will have a quick lookup
If a value hasn’t been used in a long time, might have to wait for main memory, which takes thousands of times as
long.
This does not mean you shouldn’t care about constant factors ever – they are important in real
code!
- Our theoretical tools aren’t precise enough to analyze them well.
failedChecks++ +1
}
}
return found; Approach
} -> start with basic operations, work inside out for control structures
𝑓 ( 𝑛 )=𝑛 ( 8 𝑛+1 ) + 4 - Each basic operation = +1
- Conditionals = test operations+ appropriate branch (today branches
quadratic -> O(n2)
equivalent)
- Loop = #iterations * (operations in loop body)
CSE 373 SU 19 - ROBBIE WEBER 18
5 Minutes
Your turn!
Write the specific mathematical code model for the following code and indicate the big-O
runtime in terms of .
public void foobar (int k) {
int j = 0; +1
while (j < k) { +k/5 (body) 𝑘 ( 𝑘 +2 )
𝑓 ( 𝑘 )=
for (int i = 0; i < k; i++) { +k(body) 5
System.out.println(“Hello world”); +1 quadratic ->
}
j = j + 5; +2
} Approach
-> start with basic operations, work inside out for control structures
} - Each basic operation = +1
- Conditionals = test operations+ appropriate branch (today branches
equivalent)
- Loop = #iterations * (operations in loop body)
CSE 373 SU 19 - ROBBIE WEBER 19
More Practice
Let myLL be a linked list (like we saw in lecture 1) with nodes.
Suppose we’re a client class. Let’s try to print every element of the list.
Assume get(i)takes steps
for(int i=0; i<myLL.size(); i++){
System.out.println(myLL.get(i));
}
Both will work, one might be easier to think about/code up in some instances than others.
Punchline: Iterators make your client’s code more efficient (which is what they care about)