Skip to content

Latest commit

 

History

History
277 lines (183 loc) · 10.6 KB

computer_hardware.livemd

File metadata and controls

277 lines (183 loc) · 10.6 KB

Computer Hardware

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Review Questions

Upon completing this lesson, a student should be able to answer the following questions.

  • How does the processor, memory, and storage device of a computer affect the performance of our programs?
  • How are program instructions executed on the Stack?

Overview

Computers are composed of several parts. As programmers, we are primarily concerned about the processor, and the memory.

The processor determines the computer's speed when performing calculations.

Memory or RAM (random access memory) determines the amount of data we can store during a calculation.

In software terms, this translates to the speed and memory consumption of our program. Generally as you build software, those are your main performance concerns.

It's not within the scope of the course to provide an indepth understanding of computers and computer hardware. Here's a great primer by Tom Scott to understand more about what your computer is doing under the hood.

YouTube.new("https://www.youtube.com/watch?v=Z5JC9Ve1sfI")

Memory

We store information in memory. We can think of memory as a series of cells that can each store some value.

The string "hello" might be stored in memory like so.

Under the hood, memory is stored in electrical signals called bits.

Each bit represents 1 or 0 as an on or off value for an electrical signal.

Eight bits are grouped together to make bytes. See Strings and Binaries for more.

Memory is generally measured in kilobytes (1024 bytes) megabytes (1024 kilobytes) and gigabytes (1024 megabytes).

Since they use the metric system, you might expect each to increase by 1000 instead of 1024. However, computers use binary numbers, so hardware is built based on powers of 2.

Stack Vs Heap

Memory is divided into several parts.

The stack holds functions and variables as they execute.

The heap is a free memory resource where we can put pretty much any value that we want.

For example, when you define the variable hello and bind it to "world", hello is stored on the stack and it points to the location of memory in the heap.

hello = "world"

Stack Frames

The stack is a LIFO (last in first out) queue. Instructions stored on the stack create stack frames.

As we execute functions, they are stored in a stack frame.

The last function to be added onto the stack will be the first one executed and removed from the stack.

That's why in the code below, three/0 executes, then two/1, then one/1.

one = fn _ -> IO.inspect("one") end
two = fn _ -> IO.inspect("two") end
three = fn -> IO.inspect("three") end

one.(two.(three.()))

Stack Overflow

The stack has a limited amount of memory allocated to it. When we over-allocate memory on the stack we get stack overflow. For example, if we continued executing functions in functions eventually the stack would (generally) run out of space.

Stack Overflow

Memory Consumption

Whenever we create a value it is stored in memory. If the value is too large, it can consume more memory than is available on the heap (or is allocated to the program).

For example, loading a large file's contents into memory is a performance concern.

You can see the memory this livebook consumes in the Runtime Settings panel. Press s then r to open the settings panel.

Uncomment the line below and evaluate the cell. You'll notice that the amount of memory consuption by Processes should increase. That's because the list must be stored in memory.

Comment the line again and re-evaluate the cell and you'll notice that the memory consuption goes down because the list is no longer in memory.

# list = Enum.to_list(1..1000000)

For more on Memory, there is a more indepth video by Crash Course Computer Science.

YouTube.new("https://www.youtube.com/watch?v=fpnE6UAfbtU")

Storage

Generally, we are more concerned about the memory consumption of our program. However, if our application requires that users download large files onto their computer, it's useful to be aware of storage.

Files can be saved to the hard drive (or solid state drive in more modern computers) of a computer. The hard drive is a slower (but generally larger) storage device. Unlike memory, it is often used for persistent values rather than values which only exist during the runtime of the program.

For more on memory and storage, you can watch this video by Crash Course Computer Science.

YouTube.new("https://www.youtube.com/watch?v=TQCr9RV7twk")

CPU

The CPU (Central Processing Unit) executes the instructions that you create in your program.

When you perform any operation such as 2 + 2 it is handled by the CPU.

The more instructions you provide to your program, the more work the CPU has to perform. If there are too many instructions, then the program will take longer to execute.

For example, it's far faster to perform ten addition operations than it is to perform ten million.

Try evaluating the two cells below and notice how the first is far faster (nearly instant).

You don't need to understand the specifics of Enum and that works yet. The only important difference between these examples is the size of the data. The first is for 10 elements, the second is for 10_000_000.

Enum.map(1..10, fn each -> each + 2 end)
Enum.map(1..10_000_000, fn each -> each + 2 end)

For more on the CPU there is an great video by Crash Course Computer Science.

YouTube.new("https://www.youtube.com/watch?v=FZGugFqdr60")

Conclusion

Performance issues can significantly impact a program, leading to slowness or even crashing. Always consider your program's processing and memory constraints and how this might affect your users.

In addition, performance issues generally have a more significant impact on lower-quality computers. Thus, your program may be performant on your personal computer but cause problems on lower-performance hardware.

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Computer Hardware reading"
$ git push

We're proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation