Lecture 16: Loose Ends and Software Design

July 19th, 2021


Today: Loose Ends, Software Design - Strategy and Style, bits and bytes, computer hardware CPU, RAM

Mid Quarter Evaluation

Crypto Puzzle Solution

Thanks for all the submissions! I emailed the prize winners, which were very fast, and we may have a few more emails to do for people who did not quite win.

See picture of solution sketch

Word of the day: Systematic

Looking at the encrypted text - where do you start? Pick some little piece of it. Keep notes as you work out bits of it. I think this is a very CS working pattern.

Crypto History

History aside: this is neat all-coming-together moment, looking at CS106A solution sketch.

IN World War II, this is what Enigma decryption looked like. The Code breakers had "cribs" - text they suspected would appear in the plaintext, like say the signature at the end. Then they tried endless substitutions to work out the code. Manipulating strings of characters .. is it any wonder that this is the group that laid the first steps of developing the digital computer.


Program Design and Style

We'll look over to the Python guide chapters on style readability and decomposition.

Why?

Why is the code this way? There is a reason, and today it is revealed.

Guide 1: Style Readable

Guide 2: Style Decomposition


Bits and Bytes

At the smallest scale in the computer, information is stored as bits and bytes. In this section, we'll look at how that works.

Bit

Byte

How Many Patterns With N Bits?

How many different patterns can be made with 1, 2, or 3 bits?
Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111

Number of bits Different Patterns
1 0 1
2 00 01 10 11
3 000 001 010 011
100 101 110 111
Number of bits Number of Patterns
1 2
2 4
3 8
4 16
5 32
6 64
7 128
8 256

One Byte - 256 Patterns

"HDR" Image


What is a Computer?

You have one on your person all day. You're debugging code for one. You see the output of them constantly. What is it and how does it work?

Step 1 - Why is it called Silicon Valley?

Moore's Law

We'll look starting from the outside...

Computer - CPU, RAM, Storage

alt: computer is made of CPU, RAM, storage

Extra: GPU

Want to talk about running a computer program...

1. Running Program Gets its own RAM Area

alt:each running program gets its own area in RAM

2. Operating System (Terminal)

3. RAM = Code + Vars, CPU Runs the Code

alt:program RAM area stores both code and values

4. CPU

5. CPU "Cores"

Process Manager

Browser Tab = Process


Python Shields us from Hardware Details - Great!

Python shields us from much detail about CPU and RAM, which is great. We're just peeking at the details here to get a little insight about what it means for a program to run, use CPU and RAM.

Hardware Demo Program

Hardware Squandering Program!

> hardware-demo.zip

Demo: computer is mostly idle to start. Idle CPU is cool. CPU starts running hard, generates heat .. fan spins! This program is an infinite loop, see the code below. It uses 100% of one core. Why is the fan running on my laptop? Use Activity Monitor (Mac), Task Manager (Windows) to see programs that are currently running, see CPU% and MEM%. Run program twice, once in each of 2 terminals - 200%

Core function of -cpu feature:

def use_cpu(n):
    """
    Infinite loop counting a variable 0, 1, 2...
    print a line every n (0 = no printing)
    """
    i = 0
    while True:
        if n != 0 and i % n == 0:
            print(i)
        i = i + 1

Try 1000 first ... yikes! Try 1 million instead

$ python3 hardware-demo.py -cpu 1000000
0
1000000
2000000
3000000
4000000
5000000
6000000
7000000
^CTraceback (most recent call last):
  File "hardware-demo.py", line 66, in 
    main()
  File "hardware-demo.py", line 56, in main
    use_cpu(n)
  File "hardware-demo.py", line 24, in use_cpu
    i = i + 1
KeyboardInterrupt

(ctrl-c to exit)

Let's Talk About RAM

When code reads and writes values, those values are stored in RAM. RAM is a big array of bytes, read and written by the CPU.

Say we have this code

n = 10
s = 'Hello'
lst = [1, 2, 3]
lst2 = lst

Every value in use by the program takes up space in RAM.

alt:python values each taking space in RAM

RAM

Python Values in RAM

Demo using -mem, Look in activity monitor, "mem" area, 100 = 100 MB per second. Watch our program use more and more memory of the machine. Program exits .. not in the list any more!

$ python3 hardware-demo.py -mem 100
Memory MB: 100
Memory MB: 200
Memory MB: 300
Memory MB: 400
Memory MB: 500
Memory MB: 600
Memory MB: 700
^CTraceback (most recent call last):
...
KeyboardInterrupt
(ctrl-c to exit)