本节你会了解到:
- 各种数据结构在内存中所占空间的大小
- 简单地了解一下复杂度
大多数情况下,得到运行时间的数学模型需要进行以下步骤:
- 确定输入模型,定义问题的规模
- 识别内循环
- 根据内循环中的的操作确定成本模型
- 对于给定的输入,判断这些操作的执行频率
对增长数量级的常见总结:
一般来说,二分策略
的数量级为logN,比如二分查找;分治策略
的数量级为NlogN,比如归并排序。
找到数组中三者和为零的个数
ThreeSumFast:
TwoSumFast:
2-sum问题的线性对数级别的解决
3-sum问题的快速算法
命题D:在Bag、Stack、Queue的链表实现中所有的操作在最坏情况下所需时间都是常数级别的。
在对程序进行性能分析时的注意事项:
- 大常数
- 非决定性的内循环
- 指令时间
- 系统因素
- 不分伯仲
- 对输入的强烈依赖
- 多个问题参量
内存
对象
一个Integer对象会使用24字节的空间,其中16字节的对象开销,4字节存储int值,还有4字节填充字节(padding)。
同理,一个Date对象使用32字节的空间。
链表
一个内部类的话:16字节的对象开销
+8字节的额外开销
+指向item和next对象引用各需8字节
= 40byte
数组
Java中数组被实现为对象,数组的空间包括对象所需内存加上引用所需内存。
例如,一个含有N个Date对象的数组,需要使用24字节(数组开销,包括16字节的对象开销,4字节保存长度以及4填充字节)加上8N字节(所有对象的引用)加上每个Date对象的32字节,总共(24+40N)字节
字符串对象
一个String对象一般需要40字节(16字节表示对象,三个int实例变量:偏移量、计数器、散列值个需要4字节,加上数组引用8个字节和4个填充字节)。
Q + A
Q. How do I increase the amount of memory and stack space that Java allocates?
A. You can increase the amount of memory allotted to Java by executing with java -Xmx200m Hello
where 200m means 200 megabytes. The default setting is typically 64MB. You can increase the amount of stack space allotted to Java by executing with java -Xss200k Hello
where 200k means 200 kilobytes. The default setting is typically 128KB. It’s possible to increase both the amount of memory and stack space by executing with java -Xmx200m -Xss200k Hello
.
Q. What is the purpose of padding(填充字节)?
A. Padding makes all abjects take space that is a mulitple of 8 bytes.This can waste some memory but it speeds up memory access and garbage collection(垃圾回收).
Creative Problems
14.4-sum.Develop a brute-force solution FourSum.java to the 4-sum problem.
20.Bitonic search(双调查线).An array is bitonic if it is comprised of an increasing sequence of integers followed immediately by a decreasing sequence of integers. Write a program that, given a bitonic array of N distinct int values, determines whether a given integer is in the array. Your program should use ~ 3 lg N compares in the worst case.
Answer: Use a version of binary search, as in BitonicMax.java, to find the maximum (in ~ 1 lg N compares); then use binary search to search in each piece (in ~ 1 lg N compares per piece).
web exercise
13 Search in a sorted, rotated list. Given a sorted list of N distinct integers that has been rotated an unknown number of positions,e.g., 15 36 1 7 12 13 14, write a program RotatedSortedArray.java to determine if a given integer is in the list. The order of growth of the running time of your algorithm should be log N.
31 Given an array a[] of N real numbers, design a linear-time algorithm to find the maximum value of a[j] - a[i] where j ≥ i.