Today: program style: readability and decomposition, bits and bytes, computer hardware CPU, RAM, storage

Program Design and Style

Why is code written the way it is? Today we tell the outside, strategic story, driving what forms of code work best.

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

Python Guide: Readable Code

Python Guide: 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


We'll do this if we have time.

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...

Running Program Gets its own RAM Area
"Process"

alt:each running program gets its own area in RAM

Operating System

Run Code: CPU + RAM + Code

alt:program RAM area stores both code and values

CPU

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

Nick's 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)