class StatsKeeper{
public:
//記錄倍精度數列的類別
void Add(double d); //以及快速統計的方法
private: int count; /*到目前的數 量
*/ public:
double Average();
private: double minimum;
list<double>
past_items
;double maximum;
};
//記錄倍精度數列的類別以及快速統計的方法
class StatsKeeper{
public:
void Add(double d);
double Average();
private:
list<double>pass_times;
int count; //到目前的數量
double minimum;
double maximun;
};
顯然第2個程式碼賞心悅目許多且更容易閱讀。
為了計算程式在各種網路連線速度下的行為,建立了 TcpConnectionSimulator 類別,建構子需要4個參數:
public class PerformanceTester{
public static final TcpConnectionSimulator wifi = new TcpConnectionSimulator(
500, /*kbps*/
80, /*millisecs latency*/
200, /*jitter*/
1 /*packet loss%*/);
public static final TcpConnectionSimulator t3_fiber =
new TcpConnectionSimulator(
45000, /*kbps*/
10, /*millisecs latency*/
0, /*jitter*/
0 /*packet loss%*/);
public static final TcpConnectionSimulator cell = new TcpConnectionSimulator(
100, /*kbps*/
400, /*millisecs latency*/
250, /*jitter*/
5 /*packet loss%*/);
}
我們讓程式碼一致,加入幾個換行,並將註解移到上方,所有參數移到同一行:
public class PerformanceTester{
//TcpConnectionSimulator(throughput,latency,jitter,packet_loss)
[Kbps] [ms] [ms] [percent]
public static final TcpConnectionSimulator wifi=
new TcpConnectionSimulator(500, 80, 200, 1);
public static final TcpConnectionSimulator t3_fiber=
new TcpConnectionSimulator(4500, 10, 0, 0);
public static final TcpConnectionSimulator cell=
new TcpConnectionSimulator(100, 400, 250, 5);
}