当前位置:网站首页>CPT 102_ LEC 10
CPT 102_ LEC 10
2022-04-21 12:48:00 【NONE_ WHY】
1. Cost of operations and measuring efficiency
1.1. Analysing Costs
1.1.1. How to determine the costs of a program?
- Focus
- Time
- Run the program and count the milliseconds/minutes/days
- Count the number of steps/operations the algorithm will take
- Space
- Measure the amount of memory the program occupies
- Count the number of elementary data items the algorithm stores
- Time
- Method
- Programs
- Benchmarking
- Algorithms
- Analysis
- Programs
1.2. Benchmarking: program cost
1.2.1. Measure
- Actual programs
- On real machines
- On specific input
- Measure elapsed time
- System.currentTimeMillis() → time from system clock in milliseconds (long)
- Measure real memory usage
1.2.2. Problems
- What input
- ⇒ choose test sets carefully use large data sets don’t include user input
- Other users/processes
- ⇒ minimise average over many runs
- Which computer?
- ⇒ specify details
1.3. Analysis Algorithm【 See :INT 102_LEC 02】
1.3.1. Algorithm Complexity & Asymptotic Notation
- Abstract away from the details of
- The hardware
- The operating system
- The programming language
- The compiler
- The program
- The specific input
- Measure number of “steps” as a function of the data size
- Worst case (easier)
- Average case (harder)
- Best case (easy, but useless)
- Construct an expression for the number of steps
- Simplified into terms of different powers/functions of n
- e.g.
- Drop the lower order terms (the ones that will be insignificant with large n)
- e.g.
- e.g.
- Actual constant will depend on the hardware ⇒ Drop the constant factors
- e.g.
- e.g.
- “Asymptotic cost”, or “big-O” cost
- Describes how cost grows with input size
- e.g.
1.3.3. Big Oh Notation
- Definition
- A notation for describing efficiency of computer algorithms
- Example
- Provide
- Assuming a 100-MHz clock,

- Assuming a 100-MHz clock,
- Info
- Hz : How many times in 1 second
- 100 MHz =
Hz →
Time /s → 1 Time consumption =
=
s = 10 ns
- Time
- Provide
1.3.4. Typical Costs in Big ‘O’
-
If data/input is size n,and has a 100-MHz clock How does it grow 
Constant cost is independent of n 
Logarithmic size x 10 → add a little (log(10)) to the cost 
Linear size x 10 → 10 x the cost 
N Log N size x 10 → bit more than 10 x 
Quadratic size x 10 → 100 x the cost 
Cubic size x 10 → 1000 x the cost 
Exponential adding one to size -> doubles the cost
=> You don’t want to run this algorithm!

Fartorial adding one to size -> n x the cost
=> You definitely don’t want this algorithm!
1.3.5. Common size arrangement
1.4. Step
1.4.1. Problem: What is a “step”?
- Count
- Actions in the innermost loop (happen the most times)
- Actions that happen every time round (not inside an “if”)
- Actions involving “data values” (rather than indexes)
- Representative actions (don’t need every action)
- Example
-

-
1.4.2. What’s a step: Pragmatics
- Count the most expensive actions
- Adding 2 numbers is cheap
- Raising to a power is not so cheap
- Comparing 2 strings may be expensive
- Reading a line from a file may be very expensive
- Waiting for input from a user or another program may take forever
- Sometimes we need to know how the underlying operations are implemented in the computer to analyse well
2. ArrayList: retrieve, remove, add
2.1. ArrayList: retrieve, remove, add
2.1.1. ArrayList: get, set
- Assume List contains n items
- Best, worst, average:

- Constant number of steps, regardless of n
- Best, worst, average:
2.1.2. ArrayList: remove
- Assume List contains n items
- Worst Case
- Index = 0
2.1.3. ArrayList: add
- Assume List contains n items
- Worst Case
- Index = 0
- Average case
- O(n)
2.1.4. ArrayList: add at end
- Assume List contains n items
- Worst Case
- Index = 0
- Average case
- Average over all possible states and input
- O(1)
- Amortised cost over time
- Total cost of adding n items / n
- O(1)
- Average over all possible states and input
2.1.5. Summary
-
get O(1) - Best, worst, average
- Constant number of steps, regardless of n
set O(1) remove O(n) add(at i) O(n) - Worst and average
add(at end) O(1) - Average
O(n) - Worst
O(1) - Amortised average
3. ArraySet: contains, remove, add
3.1. Cost of ArraySet
3.1.1. ArraySet uses same data structure as ArrayList
- Does not need to keep items in order
3.1.2. Operations
- dcontains(item)
- add(item) ← always add at the end
- remove(item) ← don’t need to shift down – just move last item down
3.1.3. What are the costs?
- contains: O(n)
- remove: O(1) O(n)
- add: O(1) O(n)
4. Q & A
- When analysing the cost of an algorithm, loop usually is the focus. (T or F)
- T
- Which of the following operations is more expensive?
- Reading a line from a file 【×】
- Reading a line from a user 【√】
- Worst case cost analysis is usually more difficult than average cost analysis. (T or F)
- F
版权声明
本文为[NONE_ WHY]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211243322856.html
边栏推荐
- nodejs 如何将 Buffer 数据转为 String
- 字段行相同则合并在另外一个字段sql 语句?
- How to choose the production company of product 3D display film?
- [200 opencv routines by youcans] 159 Fixed threshold method for image processing
- 使用sql语句修复表
- redis-常见问题
- Operation of simulated examination platform of test question bank for operation certificate of safety management personnel of hazardous chemical business units in 2022
- Calculate the day of the year and implement it in C language
- 53w字!阿里首推系统性能优化指南太香了,堪称性能优化最优解
- Title and answer of G3 boiler water treatment certificate in 2022
猜你喜欢

redis-常见问题

Game industry case 2: player level
2020BATJZ大厂Android高级工程师面试题-选择题合集(附答案解析)

2022年G3锅炉水处理上岗证题目及答案

2022年监理工程师考试质量、投资、进度控制练习题及答案

2022年4月中国数据库排行榜:春风拂面春意暖,分数回升四月天

只出现一次的数字 II(哈希、位操作、逻辑电路、有限状态自动机)

Best practices | improve HR workflow by using JIRA service management
![[thesis study] Yolo V2](/img/7e/ccb6717b392763f340a14155711c69.jpg)
[thesis study] Yolo V2

How to improve the ability of video playback of Jenkins
随机推荐
Modify the name of the string through the list
IPEmotion采集J1939协议信号
AES automatically generates Base64 key encryption and decryption
终于有人讲明白了!原来这才是全球低时延一张网技术
2022语言与智能技术竞赛再升级,推出NLP四大前沿任务
ASM插桩之美
Scala installation and development environment configuration tutorial
Master slave replication -- 03 -- synchronization data consistency
制造业数字化转型存在哪些问题
Machine learning-sklearn-13 (regression family - lower - nonlinear problem: polynomial regression (a new characteristic matrix is formed after polynomial transformation))
阿里天池比赛——街景字符编码识别
逆波兰表达式
AcWing 1854. 晋升计数 题解 解方程推式子
Package rpart of decision regression tree implemented by R language
Vscode often pops up: an error occurred when trying to create a file in the target directory. Try again. Skip this file and close setup
The 2022 language and intelligent technology competition was upgraded to launch four cutting-edge tasks of NLP
数据显示ETH燃烧的有多猛
2020年4面美团(多线程+redis
上个网课都能被AI分析“在走神”,英特尔这个情绪检测AI火了
选择排序法









Hz →
=
s = 10 ns























