×

算法练习(56): 计时器的设计(1.4.3)

• StdDraw库使用
• 计时器的设计

题目

1.4.3 修改DoublingTest,使用StdDraw产生类似于正文中的标准图像和对数图形,根据需要调整比例使图像总能够充满窗口的大部分区域

1.4.3 Modify DoublingTest to use StdDraw to produce plots like the standard and log-log plots in the text, rescaling as necessary so that the plot always fills a substantial portion of the window.

分析

DoublingTest会用到Stopwatch，这个类是个计时器

``````public class Stopwatch {
private final long start;
public Stopwatch() {
start = System.currentTimeMillis();
}
public double elapsedTime() {
long now = System.currentTimeMillis();
return (now - start) / 1000.0;
}
}
``````

DoublingTest 是个计算ThreeSum时间的用例

``````public class DoublingTest {
public static double timeTrial(int N) {  // Time ThreeSum2.count() for N random 6-digit ints.
int MAX = 1000000;
int[] a = new int[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform(-MAX, MAX);
Stopwatch timer = new Stopwatch();
int cnt = ThreeSum.count(a);
return timer.elapsedTime();
}

public static void main(String[] args) {  // Print table of running times.
for (int N = 250; true; N += N) {  // Print time for problem size N.
double time = timeTrial(N);
StdOut.printf("%7d %5.1f\n", N, time);
}
}
}
``````

1.X轴，Y轴大小的设置

``````static void setXscale(double x0, double x1) //reset x range to (x0 , x1)
static void setYscale(double y0, double y1) //reset y range to (y0 , y1)
``````

2.在X轴，Y轴上写数字

``````static void text(double x, double y, String s)
``````

3.设置点的颜色、大小

``````static void setPenColor(Color c)  //set pen color to c
static void setPenRadius(double r) //set pen radius to r
``````

1.绘制X轴Y轴

``````StdDraw.setXscale(-0.1,1.1);
StdDraw.setYscale(-0.1,1.1);

StdDraw.line(0,0,0,1.05);
StdDraw.line(0,0,1.05,0);
``````

``````//设置坐标系
StdDraw.setXscale(-0.1,1.1);
StdDraw.setYscale(-0.1,1.1);

StdDraw.line(0,0,0,1.05);
StdDraw.line(0,0,1.05,0);
//画刻度
for (double f = 0 ; f <= 1 ; f += 0.2 ){
DecimalFormat df = new DecimalFormat("0.0");
StdDraw.text(f,-0.06,df.format(f));
StdDraw.text(-0.06,f,df.format(f));
}
``````

``````StdDraw.setXscale(-0.1,1.1);
StdDraw.setYscale(-0.1,1.1);

StdDraw.line(0,0,0,1.05);
StdDraw.line(0,0,1.05,0);

for (double f = 0 ; f <= 1 ; f += 0.2 ){
DecimalFormat df = new DecimalFormat("0.0");
StdDraw.text(f,-0.06,df.format(f));
StdDraw.text(-0.06,f,df.format(f));

StdDraw.line(f,-0.02,f,0);
StdDraw.line(-0.02,f,0,f);
}
``````

`StdOut.printf("%7d %5.1f\n", N, time);`

`StdOut.printf("%7d %5.3f\n", N, time);`

``````250      0.017
500      0.037
1000     0.218
2000     1.770
4000     9.512
``````

``````X轴 0.0625 Y轴 0.002
X轴 0.125 Y轴 0.003
X轴 0.25 Y轴 0.022
X轴 0.5 Y轴 0.18
X轴 1 Y轴 1
``````

``````StdDraw.setXscale(-0.1,1.1);
StdDraw.setYscale(-0.1,1.1);

StdDraw.line(0,0,0,1.05);
StdDraw.line(0,0,1.05,0);

for (double f = 0 ; f <= 1 ; f += 0.2 ){
DecimalFormat df = new DecimalFormat("0.0");
StdDraw.text(f,-0.06,df.format(f));
StdDraw.text(-0.06,f,df.format(f));

StdDraw.line(f,-0.02,f,0);
StdDraw.line(-0.02,f,0,f);
}
//设置画笔大小
//画点
StdDraw.point(0.0625,0.002);
StdDraw.point(0.125,0.003);
StdDraw.point(0.25,0.022);
StdDraw.point(0.5,0.18);
StdDraw.point(1,1);
``````

答案

``````public class DoublingTest2 {

public static double timeTrial(int N) { // Time ThreeSum2.count() for N
// random 6-digit ints.
int MAX = 1000000;
int[] a = new int[N];
for (int i = 0; i < N; i++)
a[i] = StdRandom.uniform(-MAX, MAX);
Stopwatch timer = new Stopwatch();
int cnt = ThreeSum.count(a);
return timer.elapsedTime();
}

// 绘制标准图形
public static void drawStandard(int[] n, double[] time) {
StdDraw.setXscale(-0.5, 1.2 * n[n.length - 1] / 1000.0);
StdDraw.setYscale(-0.5, time[n.length - 1] * 1.2);
// 建立坐标系
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.line(0, 0, 1.1 * n[n.length - 1] / 1000, 0);
StdDraw.line(0, 0, 0, 1.1 * time[n.length - 1]);
for (int i = 1; i < 1.1 * n[n.length - 1] / 1000.0; i++)
StdDraw.text(i, -0.2, i + "K");
for (double t = 2; t < time[n.length - 1] * 1.1; t += 2)
StdDraw.text(-0.2, t, t + "");
// 绘制图像
StdDraw.setPenColor(StdDraw.BOOK_RED);
for (int i = 0; i < n.length; i++)
StdDraw.point(n[i] / 1000.0, time[i]);
}

//绘制对数图形
public static void drawLog(int[] arrN,double[] timeN)
{
//将时间转换为其对数
double[] timelog=new double[timeN.length];
for(int i=0;i<timeN.length;i++)
timelog[i]=Math.log(timeN[i]);
StdDraw.setXscale(-0.5,1.2*arrN[arrN.length-1]/1000.0);
StdDraw.setYscale(-0.5,timelog[arrN.length-1]*1.2);
//建立坐标系
StdDraw.setPenColor(StdDraw.BLACK);
StdDraw.line(0, 0, 1.1*arrN[arrN.length-1]/1000, 0);
StdDraw.line(0, 0,0, 1.1*timelog[arrN.length-1]);
for(int i=1;i<1.1*arrN[arrN.length-1]/1000.0;i++)
StdDraw.text(i,-0.2,i+"K");
for(double t=0;t<timelog[arrN.length-1]*1.1;t+=timelog[arrN.length-1]/5)
StdDraw.text(-0.2, t, String.format("%.2f",t));
//绘制图像
StdDraw.setPenColor(StdDraw.BOOK_RED);
for(int i=0;i<arrN.length;i++)
StdDraw.point(arrN[i]/1000.0, timelog[i]);
}

public static void main(String[] args) {
int MAX = 2000;
int i = 0;
int step = 10;
int len = MAX / step + 1;
int[] arrN = new int[len];
double[] timeN = new double[len];
for (int N = 0; N <= MAX; N = N + step) {
double time = timeTrial(N);
StdOut.printf("%7d %5.7f\n", N, time);
arrN[i] = N;
timeN[i++] = time;
}
drawStandard(arrN, timeN);
drawLog(arrN,timeN);
}
}
``````

代码索引

DoublingTest2.java