<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">在一亿数据的赋值下,memset使用了1ms,而for循环使用了136ms</span>
贴上代码:
#include <string.h> #include <iostream> #include <string> #include <iostream> #define MAXSIZE 10000 using namespace std; #include <time.h> #include <sys/timeb.h> int get_time(string r) { struct timeb rawtime; ftime(&rawtime); static int ms = rawtime.millitm; static unsigned long s = rawtime.time; int out_ms = rawtime.millitm - ms; unsigned long out_s = rawtime.time - s; if (out_ms < 0) { out_ms += 1000; out_s -= 1; } ms = rawtime.millitm; s = rawtime.time; int total = 1000*out_s+out_ms; cout<<r<<": "<<total<<"ms"<<endl; return total; } int main() { int (*arr)[MAXSIZE]=new int[MAXSIZE][MAXSIZE]; //MAXSIZE 10000 get_time("memset begin"); memset(arr,'9',sizeof(arr)); get_time("memset end"); get_time("for begin"); for(int i=0;i<MAXSIZE;++i) for(int j=0;j<MAXSIZE;++j)arr[i][j]=99; get_time("for end"); delete[] arr; return 0; }