史萊姆論壇

史萊姆論壇 (http://forum.slime.com.tw/)
-   程式語言討論區 (http://forum.slime.com.tw/f76.html)
-   -   請問一下為什麼一定要用clock_t 來宣告變數 (http://forum.slime.com.tw/thread222298.html)

a19870504 2007-12-19 08:05 PM

請問一下為什麼一定要用clock_t 來宣告變數
 
小弟的書上後面習題有問到說 clock_t 跟int or long int 一樣嗎?舉個例子還實驗看看

我就用了課本的第一個例題 只是把clock_t 改為 int 型態兩個比較一下 發現到

竟然可以編譯 但是最後的結果有出入

這是使用 int 型態的執行結果
y = 93.519356285
CLOCKS_PER_SEC is:
1000000
運算1000000次所需要的時間:
0.020000000sec
每次運算所花費的時間是:
2.00000000e-08sec

這是使用 clock_t 宣告的執行結果
y = 93.519356285
CLOCKS_PER_SEC is:
1000000
運算1000000次所需要的時間:
0.010000000sec
每次運算所花費的時間是:
1.00000000e-08sec

source code :
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 #include <ctime>
5 using namespace std;
6
7 int main()
8 {
9 clock_t start, finish; //只有這裡改變
10 double x = 8745.87, y;
11 double abcd ;
12 int ee = 1000000 ;
13 start = clock ();
14 for(int i=0; i < ee; i++) y =sqrt(x);
15 finish = clock();
16
17 cout << setiosflags(ios::fixed) << setiosflags(ios::right)
18 << setiosflags(ios::showpoint) << setprecision(9) ;
19 cout << "y = " << setw(10) << y << endl;
20 abcd = double(finish-start)/CLOCKS_PER_SEC;
21 cout << "CLOCKS_PER_SEC is: \n" << CLOCKS_PER_SEC << endl;
22 cout << "運算" << ee << "次所需要的時間: \n"
23 << abcd << "sec" <<endl;
24 cout << "每次運算所花費的時間是:\n"
25 << setiosflags(ios::scientific) //使用科學表示法
26 << abcd / double(ee) << "sec" << endl;
27 return 0;
28 }

問題1.為什麼我前面的CLOCKS_PER_SEC 會是1000000 而不是跟課本一樣是1000.0000000
我對照課本都一模一樣??
問題2.書上是說使用ctime這個 header file 就一定要用 clock_t 來宣告
為什麼我用其他資料型態也可以編譯且也可以執行 但是結果卻是多一倍的時間??

mini 2007-12-20 03:56 PM

引用:

作者: a19870504 (文章 1862189)
小弟的書上後面習題有問到說 clock_t 跟int or long int 一樣嗎?舉個例子還實驗看看

我就用了課本的第一個例題 只是把clock_t 改為 int 型態兩個比較一下 發現到

竟然可以編譯 但是最後的結果有出入

這是使用 int 型態的執行結果
y = 93.519356285
CLOCKS_PER_SEC is:
1000000
運算1000000次所需要的時間:
0.020000000sec
每次運算所花費的時間是:
2.00000000e-08sec

這是使用 clock_t 宣告的執行結果
y = 93.519356285
CLOCKS_PER_SEC is:
1000000
運算1000000次所需要的時間:
0.010000000sec
每次運算所花費的時間是:
1.00000000e-08sec

source code :
1 #include <iostream>
2 #include <iomanip>
3 #include <cmath>
4 #include <ctime>
5 using namespace std;
6
7 int main()
8 {
9 clock_t start, finish; //只有這裡改變
10 double x = 8745.87, y;
11 double abcd ;
12 int ee = 1000000 ;
13 start = clock ();
14 for(int i=0; i < ee; i++) y =sqrt(x);
15 finish = clock();
16
17 cout << setiosflags(ios::fixed) << setiosflags(ios::right)
18 << setiosflags(ios::showpoint) << setprecision(9) ;
19 cout << "y = " << setw(10) << y << endl;
20 abcd = double(finish-start)/CLOCKS_PER_SEC;
21 cout << "CLOCKS_PER_SEC is: \n" << CLOCKS_PER_SEC << endl;
22 cout << "運算" << ee << "次所需要的時間: \n"
23 << abcd << "sec" <<endl;
24 cout << "每次運算所花費的時間是:\n"
25 << setiosflags(ios::scientific) //使用科學表示法
26 << abcd / double(ee) << "sec" << endl;
27 return 0;
28 }

問題1.為什麼我前面的CLOCKS_PER_SEC 會是1000000 而不是跟課本一樣是1000.0000000
我對照課本都一模一樣??
問題2.書上是說使用ctime這個 header file 就一定要用 clock_t 來宣告
為什麼我用其他資料型態也可以編譯且也可以執行 但是結果卻是多一倍的時間??

Q1:
理應是 1000,請搜尋你的 C++開發軟體之 time.h (ctime是由 time.h衍申而來)
沒有意外的話其定義是
#define CLOCKS_PER_SEC ((clock_t)1000)
而 clock_t 是
typedef long clock_t;

所以
clock_t 是 long 型態的別名
CLOCKS_PER_SEC 其實就是 1000

Q2:
既然 clock_t 其實就是 long 型態的別名
所以只要 finish = clock();
可以通過編譯
改成 其他資料型態 ※ 當然沒問題
至於
為何會多花時間
請多試幾次看看可能會有不同結果
(可能與 clock() 精密度有關)


在目前的 32 位元個人電腦上, long int 其實和 int 一樣, 都是用 32 位元 (4 bytes) 來記錄整數值
(範圍都是
int 2147483647 ~ -2147483648
long 2147483647 ~ -2147483648 )。


所有時間均為台北時間。現在的時間是 09:21 AM

Powered by vBulletin® 版本 3.6.8
版權所有 ©2000 - 2024, Jelsoft Enterprises Ltd.

『服務條款』

* 有問題不知道該怎麼解決嗎?請聯絡本站的系統管理員 *


SEO by vBSEO 3.6.1