Commit ef6e12bb by Eric Krawczyk

### lecture 3, aww yiss

parent 3e3a57d3
 # EECS 281 - 10 SEP 2015 ## Make, Complexity, Runtime ## Make, Complexity #### Introductions - survey on ctools test center ... ...
2015-09-15.md 0 → 100644
 # EECS 281 - 15 SEP 2015 ## Complexity (cont), Runtime #### Complexity - Big-O, Theta, and Omega - `Big-O`: asymptotic upper bound - `Big-Θ`: asymptotic tight bound - `Big-Ω`: asymptotic lower bound - Examples: - see slide 21-22 of `02_Complexity_Analysis.pdf` - Amortized Complexity - worst case Complexity - read up on dis - Exercise: - n balls of equal weight, except one is heaver - *O(n2)*: iterate through each ball, comparing each ball to every other ball. *`n(n-1)/2`* - *O(n)*: iterate through each ball, stopping when you find weight that is greater (or less) than original weight. - *O(log n)*: - *O(log3n)* split into three groups; compare two groups; if equal, third group has the ball, else the heavier side has the ball. omit the lighter groups, and split the new group into three. repeat until single heaviest ball is found - *O(log2n)* alternatively, split into two groups, with remainder as another group. - Job interview question - compute power ```c++ int power(int x, unsigned int y); //return x^y ``` - obvious solution uses y-1 multiplications - less obvious is log n - HEY THINK ABOUT IT. - recap - *`n`* = input size - *`f(n)`* = max number of steps for input n - *`O(f(n))`* = asymptotic upper bound #### Runtime - Calculate time in the shell ```sh /usr/bin/time progName -opts args ``` - produces a runtime report - user: time spent by program - system: time spent by OS - elapsed: time from start to finish of call - CPU: percentage of cpu devoted to process - `(user + system) / elapsed` - /usr/bin/time dd if=/dev/zero of=/dev/null - records time - `dd` (disk dump, copies data) - infile - `/dev/zero` is infinite `0` data - outfile - `/dev/null` is infinite sink - Calculate time progrmatically - OS Dependent time with rusage - using c ```c // solution in C #include #include void main() { struct rusage startu; struct rusage endu; getrusage(RUSAGE_SELF, &startu); // do yo shit right hurr getrusage(RUSAGE_SELF, &endu); double start_sec = startu.ru_utime.tv_sec + startu.ru_utime.tv_usec/1000000.0; double end_sec = endu.ru_utime.tv_sec + endu.ru_utime.tv_usec/1000000.0; double duration = end_sec - start_sec; } ``` - solution in C++ ```c++ // solution in C++ #include #include class Timer { private: struct rusage startu; struct rusage endu; double duration; public: Timer() { getrusage(RUSAGE_SELF, &startu); } void recordTime() { getrusage(RUSAGE_SELF, &endu); double start_sec = startu.ru_utime.tv_sec + startu.ru_utime.tv_usec/1e7; double end_sec = endu.ru_utime.tv_sec +endu.ru_utime.tv_usec/1e7; duration = end_sec - start_sec; } double getTime() { return duration; } }; ``` - send to cerr for autograder to ignore - don't call getTime too often, or it will slow down your code - Speed of I/O - `printf()` - `cout << "\n"` - `cout << endl;` - which one is the fastest? It's `printf` - `\n` fills buffer - slowest? `endl` (flushes buffer upon endl) - `sync_with_stdio` #### PseudoCode - `if input[i] = '(' or '[' or '{'` - `if input[i] == '(' || '[' || '{'` - always true; logically `or` with nonzero - Language-agnostic approach - in theory... - efficiency is independent of language - efficiency is dependent on algorithmic implementation - in practice... - languages can cause slowdowns - asymptotic complexity is Language-agnostic - CLRS has great pseudocode standards
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!