[問題] memset和for 初始化資料速度比較
看板C_and_CPP (C/C++)作者MashiroKinji (MashiroKinji)時間11年前 (2015/02/16 05:27)推噓1(1推 0噓 14→)留言15則, 7人參與討論串1/1
先貼上代碼
使用編譯器是Visual C++
電腦32位元
#include <stdio.h>
#include <memory.h>
#include <stdlib.h>
#include <time.h>
#define len 1000
#define times 10000
#define type long long
type c[len][len];
int main()
{
int timer[2];
timer[0] = clock();
int i = times, j = times;
while (i--)
{
memset(c, 1, sizeof(type)*len*len);
}
timer[1] = clock();
printf("%f\n", double(timer[1] - timer[0]) / CLOCKS_PER_SEC);
timer[0] = clock();
while (j--)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
c[i][j] = 0;
}
}
}
timer[1] = clock();
printf("%f\n", double(timer[1] - timer[0]) / CLOCKS_PER_SEC);
system("pause");
}
還想請問大大們一些問題
1.當我define type為bool char int 速度都差不多反而是迴圈有快一些些
當我設定為long long的時候
迴圈的做法時間會暴增這是為什麼?
我本來預想結果是
bool memset 快
char 一樣
int for 快
主要是因為我以為形態越大對於用迴圈的作法是不是更有利
2.當我把c變數設定在全域時才測的出來數值放在main裡面的話怎麼測都是0
是不是全域變數存取會比較久?
3.class和struct是不是不需要另外去清0?我測試的時候所有成員變數預設都是0耶
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 121.254.69.174
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1424035672.A.94E.html
→
02/16 08:01, , 1F
02/16 08:01, 1F
→
02/16 08:02, , 2F
02/16 08:02, 2F
→
02/16 08:02, , 3F
02/16 08:02, 3F
→
02/16 08:03, , 4F
02/16 08:03, 4F
→
02/16 08:04, , 5F
02/16 08:04, 5F
→
02/16 08:05, , 6F
02/16 08:05, 6F
→
02/16 11:48, , 7F
02/16 11:48, 7F
→
02/16 14:00, , 8F
02/16 14:00, 8F
→
02/16 14:01, , 9F
02/16 14:01, 9F
→
02/16 14:02, , 10F
02/16 14:02, 10F
→
02/16 14:04, , 11F
02/16 14:04, 11F
推
02/16 18:10, , 12F
02/16 18:10, 12F
→
02/16 20:48, , 13F
02/16 20:48, 13F
→
02/17 00:59, , 14F
02/17 00:59, 14F
→
02/17 01:00, , 15F
02/17 01:00, 15F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章