memset引出的問題

2021-05-17 09:31:09 字數 1645 閱讀 7134

寫的那個介面顯示程式裡有個復位的過程,也就是把相關的資料清0,當時用的是memset,忽然一想,這函式的開銷怎麼樣,跟直接賦值,哪個效率更高?網上搜了個程式,改了改,the code:

#include

#include

#include

#include

void use_memset(int *buff)

void use_array(int* buff)

int main(int argc, char** argv)

執行結果

in heap:

6.000000

3.000000

in stack:

2.000000

3.000000

ove如果把陣列大小改為1024

in heap:

5.000000

9.000000

in stack:

2.000000

5.000000

over

memset的優勢就顯現出來了,儘管是那麼的。。。。

如果把陣列改為1024*1024in heap:

2999.000000

4812.000000

in stack:

3558.000000

6627.000000

over

可見memset比賦值基本快一倍。

資料量比較小時賦值快

還有對stack和heap的賦值時間也不一樣,這是為什麼?

程式還有不完善的地方,沒有考慮其他程序的搶占,ok,後面是設定程序實時的**:

#include

#include

#include

#include

#include

#include

#include

void use_memset(int *buff)

void use_array(int* buff)

int main(int argc, char** argv)

struct sched_param param;

param.sched_priority=20;

if(sched_setscheduler(0,sched_fifo,¶m)!=0)

printf("in heap:/n");

use_memset(buf1);

use_array(buf1);

printf("in stack:/n");

use_memset(buf);

use_array(buf);

printf("/nover/n");

}in heap:

3064.000000

4757.000000

in stack:

3830.000000

4750.000000

over

in heap:

8796.000000

15562.000000

in stack:

8660.000000

15437.000000

over

這兩組資料 波動為什麼這麼大??實時程序後,時間怎麼不減反增??哪位能解釋下?

至於椎棧的差別到沒有了。看來是排程的問題。

Boolean TryParse引出的問題

今天專案中要用到為某個bool值屬性設定預設值為true的功能,在專案中我是這樣寫的 是否改變地圖滑鼠樣式 defaultvalue true public bool ischangecursor viewstate ischangecursor bool result true if boolea...

memset的效率問題

經過astyle格式化 void memset dstpp,c,len void dstpp int c size t len 前幾行等同於 void memset void dstpp,int c,size t len xlen len opsiz 8 while xlen 0 2號 while ...

mysql 刪表引出的問題

將測試環境的表同步到另外乙個資料庫伺服器中,但有些表裡面資料巨大,其實不同步該錶的資料就行,當時沒想太多 幾千萬的資料!1.既然已經把資料同步過來的話,那就直接delete掉就行,多大的事呢?於是 delete from table name where1 1 結果傻眼了,執行了一會兒就卡死了,對卡...