exam Flashcards

1
Q

what does a profiler do

A

helps identify how long code takes to run
- functions that are called the most
- functions that run the longest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

what are two reasons we may use way too much data

A
  1. sparse data sets
    - lots of gaps/spaces
  2. high volume of data
    - should be compressed
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

what can we do to optimize space?

A
  • compression
  • bit manip
  • choose better data structures
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

what is compression

A

analyzing the data to find and remove redundant or useless info

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

describe two types of compression

A

lossless - does not lose any info (zip, flac)

lossy - loses some info (jpeg, mp3)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

with compression, what are we sacrificing? what are we improving?

explain.

A

we are sacrificing time to improve how much space we use.

  • it takes extra time to compress, and to get the original data back
  • it takes less space because compression gets rid of redundant or useless info
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

your program is taking too much space. what are your options to fix this problem and what should you try first?

A
  1. change data structure to something that fits the problem better
  2. bit manipulation if you need to squeeze out the last few bits
  3. compression
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what type of variables should be used with bit manipulation? why?

A

unsigned variables

  • unsigned variables can’t be negative
    • overflow will be to 0
    • bit manipulation is more reliable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

what are the 3 steps to running a profiler

A
  1. compile code with flag
    - gprof flag: -pg
  2. run code
  3. run profiler
    - gprof myCode
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

what are 4 key routes to optimize your code

A
  1. change algorithms
  2. restructure code
  3. redesign data structures
  4. refactor code
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

when and why should you optimize your code? When shouldn’t you?

A

only optimize if there’s a big performance issue. otherwise, the code is likely “good enough”

optimizing often makes code harder to:
- maintain
- reuse
- understand

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

optimizing code often makes the code harder to: (3)

A
  • maintain
  • reuse
  • understand
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

what is the 80-20 rule and what concept does it relate to?

A

relates to optimization

80% of the program’s execution is spent in 20% of the code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

optimizing is about making our code do ______(more/less) work, not making the current work __________(faster/slower)

A

less

faster

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

explain and give examples of optimizing by pre-computation

A
  • pre-compute and store frequently used info in a table or variable

examples:
- constants (like PI or a BUFFER)
- the length of a string or array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

explain optimizing by data structures

A
  • select data structure that best applies to the problem. are insertions frequent? searches? sorted or not?

linked list
- fast insertions

array
- fast searches
- good for sequential processing

17
Q

optimization by caching trades ____ for _____

A

trades storage for speed

18
Q

malloc allocates a number of
____________ memory locations

the compiler ________ the type on the bits

A

contiguous
- an array

overlays

18
Q

void pointers are addresses that point at ________

19
Q

garbage collection is a form of ____________ memory management.

it attempts to reclaim _____________ which is…

A

automatic

garbage. memory occupied by objects that are no longer in use

20
Q

what does the type of a pointer actually do?

A

tells us how to interpret the data we find at the place in memory it points to

21
Q

what are three methods of garbage collection?

A
  1. tracing
    - mark and sweep. recursively determine which pointers are REACHABLE and then get rid of the ones that aren’t (NULL pointers can be reachable or unreachable)
  2. reference counting
    - keep track of how many references to an object are currently in use
    - 0 refs = garbage
  3. escape analysis
    - compiler optimization technique
    - objects that are not accessible outside of a function can get moved to the stack where they will automatically be claimed
22
Q

when should we collect garbage?

A

asap (proactive)

only when necessary (lazy)
- not enough space

somewhere in between

23
Q

what is fragmentation? how do we solve this?

A

when space in a program is used inefficiently
- there’s spaces between memory

defragmentation is used to reorganize and store data in contiguous memory

24
Q

what is a class? what does it include?

A
  • a blueprint to create objects
  • includes data/info and behaviour
25
Q

what is inheritance?

A

the traits a subclass gets from its parent

26
Q

what is polymorphism? what’s a tool we can use in C to simulate polymorphism? examples?

A

polymorphism is an object that can take on various forms

we use “union”

examples:
- shapes (circle, square, triangle)
- a man (father, husband, employee)

27
Q

the sizeof a union is the size of _____________

A

its widest type

28
Q

what is the main difference between a union and structs? explain

A

only one property within the union can be used at a time

the members of a UNION are stored at the SAME address in memory. the members of a STRUCT are stored at DIFFERENT addresses

29
Q

what is a function pointer?

show how you declare and initialize one

A

a function pointer contains the address of a function

int func1(int x); // function

int (*func1_ptr)(int x); // declare
func1_ptr = func1; // initialize

notes about initialization:
- no parentheses
- values are populated at linking stage

30
Q

unions can be used to save ________ in structures.

explain how. examples?

A

space

if all the info inside a struct doesn’t apply to all uses of the struct, a union can be created within the struct to handle each specific case

example from slides: catalog item struct
- each item has a stock number, price, and item type
BUT
- books would have an author and a title variable
- a shirt would have colour, and size variables
- a mug would have colour, but may not have size

31
Q

Unions can be used to create data structures that contain a mixture of data of different types. What can we use to differentiate between the types?

A
  • a tag field

example from slides:
#define INT_KIND 0
#define DOUBLE_KIND 1

typedef struct {
int kind; /* tag field */
union {
int i;
double d;
} u;
} Number;

// assigning
n.kind = INT_KIND;
n.u.i = 82;

32
Q

c supports 3 forms of pointer arithmetic. what are they?

A

adding an integer to a pointer
- if p points to a[i], then p + k points to a[i+k]

subtracting an integer from a pointer
- same thing but subtracting

subtracting one pointer from another: gives distance between the two elements
- if p points to a[i] and q points to a[k], then p - q is equal to i-k

33
Q

you don’t have to cache Everything,

what should you cache? (2)

A
  • you can cache the things that are used most
  • cache the things that have been used recently (?)