Theory versus Experiment: Analysis and Measurements of Allocation Costs in Sorting (With Hints for Applying Similar Techniques to Garbage Collection)

Similar documents
Chapter 1 Review of Equations and Inequalities

Discrete Structures Proofwriting Checklist

In other words, we are interested in what is happening to the y values as we get really large x values and as we get really small x values.

Regression, part II. I. What does it all mean? A) Notice that so far all we ve done is math.

Alex s Guide to Word Problems and Linear Equations Following Glencoe Algebra 1

ACCESS TO SCIENCE, ENGINEERING AND AGRICULTURE: MATHEMATICS 1 MATH00030 SEMESTER / Lines and Their Equations

Do students sleep the recommended 8 hours a night on average?

Quadratic Equations Part I

Hypothesis testing I. - In particular, we are talking about statistical hypotheses. [get everyone s finger length!] n =

AP Statistics. Chapter 6 Scatterplots, Association, and Correlation

STEP Support Programme. Hints and Partial Solutions for Assignment 1

DIFFERENTIAL EQUATIONS

Descriptive Statistics (And a little bit on rounding and significant digits)

Solving with Absolute Value

The Gram-Schmidt Process

STARTING WITH CONFIDENCE

Physics Motion Math. (Read objectives on screen.)

Solution Guide for Chapter 10

Finite Mathematics : A Business Approach

Algebra. Here are a couple of warnings to my students who may be here to get a copy of what happened on a day that you missed.

Section 20: Arrow Diagrams on the Integers

Boyle s Law and Charles Law Activity

#29: Logarithm review May 16, 2009

Main topics for the First Midterm Exam

Getting Started with Communications Engineering

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Lab Slide Rules and Log Scales

N/4 + N/2 + N = 2N 2.

Algebra Exam. Solutions and Grading Guide

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2018

Edexcel AS and A Level Mathematics Year 1/AS - Pure Mathematics

Biostatistics and Design of Experiments Prof. Mukesh Doble Department of Biotechnology Indian Institute of Technology, Madras

CSE332: Data Structures & Parallelism Lecture 2: Algorithm Analysis. Ruth Anderson Winter 2019

Graphs. 1. Graph paper 2. Ruler

Chapter 7. Scatterplots, Association, and Correlation. Copyright 2010 Pearson Education, Inc.

Chapter Usual types of questions Tips What can go ugly. and, common denominator will be

Chapter 1: January 26 January 30

Linear Algebra, Summer 2011, pt. 2

change in position change in time

8 Square matrices continued: Determinants

Introduction to Algorithms / Algorithms I Lecturer: Michael Dinitz Topic: Intro to Learning Theory Date: 12/8/16

LAB 3: VELOCITY AND ACCELERATION

Regression, Part I. - In correlation, it would be irrelevant if we changed the axes on our graph.

Conceptual Explanations: Simultaneous Equations Distance, rate, and time

An introduction to plotting data

ASTRO 114 Lecture Okay. We re now gonna continue discussing and conclude discussing the entire

COSMOS: Making Robots and Making Robots Intelligent Lecture 3: Introduction to discrete-time dynamics

1.4 Techniques of Integration

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 40

Continuity and One-Sided Limits

Stat 5421 Lecture Notes Fuzzy P-Values and Confidence Intervals Charles J. Geyer March 12, Discreteness versus Hypothesis Tests

HOW TO GET A GOOD GRADE ON THE MME 2273B FLUID MECHANICS 1 EXAM. Common mistakes made on the final exam and how to avoid them

Chapter 7 Summary Scatterplots, Association, and Correlation

Volume vs. Diameter. Teacher Lab Discussion. Overview. Picture, Data Table, and Graph

Lesson 21 Not So Dramatic Quadratics

College Physics 201 Graphing Review and Analysis

Knots, Coloring and Applications

. Divide common fractions: 12 a. = a b. = a b b. = ac b c. 10. State that x 3 means x x x, . Cancel common factors in a fraction : 2a2 b 3.

Calculus II. Calculus II tends to be a very difficult course for many students. There are many reasons for this.

The Derivative of a Function

Correlation A relationship between two variables As one goes up, the other changes in a predictable way (either mostly goes up or mostly goes down)

Math 111, Introduction to the Calculus, Fall 2011 Midterm I Practice Exam 1 Solutions

1.3 Rate of Change and Initial Value

Computer Problems for Fourier Series and Transforms

Comp 11 Lectures. Mike Shah. July 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures July 26, / 45

ASTRO 114 Lecture Okay. What we re going to discuss today are what we call radiation laws. We ve

appstats27.notebook April 06, 2017

STEP 1: Ask Do I know the SLOPE of the line? (Notice how it s needed for both!) YES! NO! But, I have two NO! But, my line is

=.55 = = 5.05

5-Sep-15 PHYS101-2 GRAPHING

AP Physics 1 Summer Assignment

Problem Set 1. CSE 373 Spring Out: February 9, 2016

Multiple Representations: Equations to Tables and Graphs Transcript

2.9 Motion in Two Dimensions

LAB Exercise #4 - Answers The Traction Vector and Stress Tensor. Introduction. Format of lab. Preparation reading

Collisions. Objective. Introduction

Computational Complexity. This lecture. Notes. Lecture 02 - Basic Complexity Analysis. Tom Kelsey & Susmit Sarkar. Notes

Chapter 6. September 17, Please pick up a calculator and take out paper and something to write with. Association and Correlation.

MATH 408N PRACTICE MIDTERM 1

Bridging the gap between GCSE and A level mathematics

Coulomb s Law. 1 Equipment. 2 Introduction

MI 4 Mathematical Induction Name. Mathematical Induction

Chapter 27 Summary Inferences for Regression

Algebra & Trig Review

ENV Laboratory 1: Quadrant Sampling

MAT224 Practice Exercises - Week 7.5 Fall Comments

[Disclaimer: This is not a complete list of everything you need to know, just some of the topics that gave people difficulty.]

Module 3 Study Guide. GCF Method: Notice that a polynomial like 2x 2 8 xy+9 y 2 can't be factored by this method.

Warm-up Using the given data Create a scatterplot Find the regression line

Multiple Regression Theory 2006 Samuel L. Baker

The degree of the polynomial function is n. We call the term the leading term, and is called the leading coefficient. 0 =

Quantum Entanglement. Chapter Introduction. 8.2 Entangled Two-Particle States

Please bring the task to your first physics lesson and hand it to the teacher.

Figure 1. Distance depends upon time.

When we use asymptotic notation within an expression, the asymptotic notation is shorthand for an unspecified function satisfying the relation:

The Inductive Proof Template

Section 2: Limits and Continuity

Modern Algebra Prof. Manindra Agrawal Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

X. Numerical Methods

of 8 28/11/ :25

Transcription:

Theory versus Experiment: Analysis and Measurements of Allocation Costs in Sorting (With Hints for Applying Similar Techniques to Garbage Collection) CS 152 Staff February, 2006 Introduction This handout illustrates techniques you can use to compare experimental data with theoretical predictions. We have chosen units so that theoretical and experimental results can be compared directly, with no curve fitting. We present theoretical and experimental results on the same graph. We use a single graph to present the cumulative results of many experiments. We plot a single point for each experiment, using different point markers for different kinds of experiments. As a simple illustrative problem, we study answers to the question how many cons cells are required to sort a list in µscheme? Insertion sort To predict the cost of insertion sort, consider I n, the number of cons cells allocated by insertion sort on a list of length n. We first sort the tail at cost I n 1, then insert the new element. If the new element is chosen at random, then on average it goes into the middle of the list, at an expected cost of 1 2n cons cells. So I n = I n 1 + 1 2 n, and this equation has the nearly exact solution I n = 1 4 n2. To test the validity of this prediction, we have run three classes of experiments: Insertion sort on a random list Insertion sort on a sorted list Insertion sort on a reverse-sorted list 1

For each experiment we have run a single sort on lists of sizes ranging from 5 to 500 elements, by fives. The results are as shown. 75000 70000 65000 60000 55000 Cons cells allocated 50000 45000 40000 35000 30000 25000 20000 15000 10000 5000 0 0 50 100 150 200 250 300 350 400 450 500 List size Cons Allocation for Insertion Sort Random list Reversed list Sorted list Theoretical prediction The experimental results show that our simple analysis is very good at predicting the costs of sorting random lists, but terrible at predicting the costs of sorting sorted or reverse-sorted lists. Obviously further theoretical work is required to explain the dramatic differences in costs. 2

The costs for the sorted lists, although they appear to be zero, are actually nonzero, but the growth is so slow that it is indistinguishable on this graph. Another graph is probably called for. One idea is to use a log-log scale, on which any power-law curve appears as a straight line. 1 10000 Cons cells allocated 1000 Random list Reversed list Sorted list Theoretical prediction 100 10 10 20 50 100 200 500 List size Cons Allocation for Insertion Sort The new graph makes the cost differences clearer: the random list and reversed list have the same slope as the theoretical curve, making both O(n 2 ), but with different constants. The sorted list also has a straight line, but with a different slope; this curve shows O(n) behavior. These insights should be enough to guide us to a more exact theoretical analysis. Merge sort The analysis of merge sort is similar. To sort a list of length n, we split it into two lists of length 1 2 n, which requires allocating n cons cells to hold the two new lists. After recursively sorting these lists, we merge the results, costing another n cons cells. The recurrence equation for the total cost of merge sort is therefore M n = 2n+2M n 2. This equation has an approximate solution M n 2nlog n. Interestingly, unlike the analysis of insertion sort, this analysis of merge sort doesn t make any assumptions about the ordering of list elements. 1 A power law is one in which y = kx n. This means that logy = logk + nlogx, so on a log-log plot a power-law appears as a straight line with slope n. 3

We perform the same experiments as above, with these results: 27000 24000 21000 Cons cells allocated 18000 15000 12000 9000 Random list Reversed list Sorted list Theoretical prediction 6000 3000 0 0 120 240 360 480 600 720 840 960 1080 1200 List size Cons Allocation for Merge Sort Here we have a more interesting result: the cost of merge sorting does indeed depend only on the size of the list, not on the order of the elements. But the cost is not exactly what we predicted. There are two possibilities: either our approximate solution to the recurrence on M n is not good enough, or our implementation allocates cells that aren t accounted for in our analysis. Careful study of the implementation should reveal which is the case. 4

How much data is enough? To make graphs, we had to choose The range of list sizes to be explored The intervals between list sizes in different parts of the range The total number of points in the graph How did we make these choices? To begin, we were guided by our theoretical analyses. Such analyses lead to hypotheses about how a program is going to behave under various circumstances. A measurement has to gather enough data to validate or invalidate the hypotheses. When picking a range, we have to make sure that test cases are big enough to hide small overheads that might not be accounted for in our analyses, but small enough to complete in a reasonable amount of time. In the case of sorting, it s easy to gather lots of data as long as the lists aren t too big. So we chose a range of list sizes such that even the slowest test in our case the longest list would complete in a reasonable amount of time. We don t have to worry much about overheads because we re measuring cons cells, and there aren t a lot of hidden cons s floating around. Had we been measuring CPU time, we would have been better off with larger test cases, because there is a lot of hidden CPU overhead floating around the calls and returns. What about density, i.e., the intervals between test cases? The sort curves don t jump around violently: they are nice smooth curves that are O(n 2 ) or O(nlogn). Because each individual measurement is cheap, it s easy enough to make the intervals uniform. But if each individual measurement is very expensive, we could cut corners by putting most of the measurements where the curves are steepest, which means large n. (But it s not an exact science. If we want to use the log-log scale, the slopes are flat, and we should have n coordinates evenly distant in log space, which means logarithmically far apart, which means putting most of the measurements at small n.) To choose the total number of points in a graph is the most difficult decision. You have to have enough points so you can make an informed judgment about whether your measurement actually follows your theoretical prediction. The sorting graphs in this paper have about 50 measurements apiece, which gives a lot of information probably more than is needed. But if we had only 10 measurements, that would definitely be too few. It really depends on the shape of the curve. To see these principles in action, let s consider a more wacky example: suppose your analysis produces a curve that looks like y = (x+10)2 (x 5) 4. This thing is going to tail off to near nothing at zero and for large values of x, so you ll need just a few confirmation points at small and large x. But the function has a nasty spike at x = 5, so you ll need to gather a substantial amount of data near x = 5 to confirm, for example, that the denominator is raised to the fourth power and not the sixth. For a garbage collector, we suggest reasonable coverage for a sensible range of γ enough to confirm or invalidate your model. We also suggest you gather some points outside the sensible range, to be sure you understand what is happening there. 5

Tools Gathering data The code for the course includes a function gammadesired that will help you set γ in your collector. By using this function, you can change γ from within µscheme itself, simply by setting the µscheme variable &gamma-desired. It would be a big mistake to recompile your collector each time you want to change γ. It can be quite useful to write shell scripts that create test cases. The course staff recommend learning Bourne shell (sh) and Awk, but many students get good results using Perl. You might have a look at our inserttest and mergetest scripts to see how they work. To learn sh, An Introduction to the Unix Shell by S. R. Bourne is more helpful than a man page. To learn Awk, try Awk A Pattern Scanning and Processing Language (Second Edition) by A. Aho, B. W. Kernighan, and P. J. Weinberger. Both of these documents are part of the BSD Unix User s Supplementary Documents and should be available for free on the Internet. There is also a book by Aho, Kernighan, and Weinberger. Drawing graphs Many students report that it is very tedious and time-consuming to try to produce decent graphs using Microsoft Excel. The graphs in this paper were produced with jgraph, and the jgraph sources for these graphs are available online. We have had very good luck with jgraph, which can be freely downloaded from the Internet and which is installed on the FAS nice servers in /usr/local/bin. 2 We have also heard good things about gnuplot, although we can t confirm these from our own experience. Avoid common mistakes Comparing theoretical predictions with experimental results requires thought. The following advice will help you avoid mistakes that students have made in the past. If your theory predicts experiment to within a factor of 2, don t just say the theory fits pretty well. Factors of 2 demand explanation. Don t overlook smaller deviations. For example, something is clearly wrong with our prediction of the cost of merge sort above. Further investigation is needed to figure out what the problem is. When you re trying to understand your results, don t overlook the load that your test cases place on the memory-management system. You might find it useful to construct different kinds of test cases. One interesting test case is one in which every allocated cell lives forever. Another is one in which every allocated cell dies quickly. You can imagine others. If your theory does not indicate that y = ax b, do not fit your experimental data to a curve of the form y = ax b. Don t forget to say what you are measuring. A label on a y axis is usually sufficient. Don t compare quantities that come in different units. For example, if your theory predicts comparisons, and your experiment measures allocations, it is meaningless to compare the two. 3 2 If you compile jgraph from sources, be sure to set -DLCC as described in the Makefile. If you do not, you will get mysterious failures when you try to view Jgraph s output. 3 Unless you have additional theory that relates comparisons to allocations, of course. 6

Don t add quantities that come in different units. For example, computing total sorting work by adding comparisons and allocations is meaningless. Don t graph your theoretical prediction as if it were just another experiment. It is quite useful to use a single graph to compare multiple experiments with theory, but only if the theoretical prediction is clearly distinguishable. Don t use your theoretical prediction as a substitute for measurement. For example, if you are trying to predict cons cells, and if your theory says the number of cons cells is 1 4 n2, it is not useful to run your code and measure 1 4 n2 you must measure the number of calls to cons. Don t hide all the interesting behavior by using the wrong scale on your graphs. If your theory says y = 1 x, and you graph y on a linear scale of 0 to 10, it will be impossible to compare theory and experiment for values of x much greater than 1. For some graphs, a log-linear or log-log plot may help with this problem. You could also consider showing both y and 10y on the same graph. Don t throw out interesting data in order to fit the rest on a reasonable scale. If your theory says y = 1 x, you definitely want to see what happens near the singularity at x = 0. Points near x = 0 should not be dismissed as outliers. 7