// C 的寫法, in second,只能精準到秒
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
int main(int argc, char *argv[]){
time_t start;
time_t end;
start = time (&start);
// do something here
end = time (&end);
cout <<"time:" << end - start << endl;
}
// C++的寫法,in millisecond,可計算到零點多秒
#include "stdio.h"
#include "time.h"
#include "stdlib.h"
int main(int argc, char *argv[]){
clock_t start_time, end_time;
float total_time = 0;
start_time = clock(); /* mircosecond */
//do something here
end_time = clock();
/* CLOCKS_PER_SEC is defined at time.h */
total_time = (float)(end_time - start_time)/CLOCKS_PER_SEC;
printf("Time : %f sec \n", total_time);
}
2010年6月30日 星期三
[C++]計算程式執行時間
兩個解決方案
2010年2月24日 星期三
2010年2月18日 星期四
[C++]將字串轉換為字元array
在C++中若要將字串物件轉換為字元陣列型態
好像不能直接轉換?! (若有方法請高手指點)
需使用到string類別中的 c_str 靜態方法,將字串物件傳回一個指標(指向字元陣列)
str.c_str();
配合strcpy複製到新的字元陣列
char * strcpy ( char * destination, const char * source );
然後才能用C++裡面沒有split字串的函數....strtok
char * strtok ( char * str, const char * delimiters );
每次呼叫strtok()...pointer會逐次往後到被切開的字串(實際上是字元陣列)起始位置
2010年2月17日 星期三
訂閱:
文章 (Atom)