memcpy()內存拷貝和賦值操作效率測試

比較memcpy()內存拷貝和"="賦值操作效率,測試代碼如下

#include <stdio.h>
#include <malloc.h>
#include <windows.h>

#define QueryFreAndCounter(m_fre,tt) QueryPerformanceFrequency(&m_fre);\
	QueryPerformanceCounter(&tt);
#define  Timecost(t2,t1,fre,tt1)\
	QueryPerformanceCounter(&t2);\
	tt1=(double)1000.0*(t2.QuadPart - t1.QuadPart) / fre.QuadPart;

using namespace std;

int main()
{
	double TimeCost = 0;
	LARGE_INTEGER m_frequency = { 0 }, m_time1 = { 0 }, m_time2 = { 0 };
	int t = 10000,t1=10000;
	int n = 10;
	int a[10] = { 1 };
	int b[10];
	QueryFreAndCounter(m_frequency, m_time1);
	while (t)
	{
		memcpy(b, a, n);
		--t;
	}
	Timecost(m_time2, m_time1, m_frequency, TimeCost);
	printf("memcpy cost %.3f ms\n", TimeCost);

	QueryFreAndCounter(m_frequency, m_time1);
	while (t1)
	{
		for (int i = 0; i < n; ++i)
		{
			b[i] = a[i];
		}
		--t1;
	}
	Timecost(m_time2, m_time1, m_frequency, TimeCost);
	printf("assignment cost %.3f ms\n", TimeCost);
	system("pause");
}

運行結果:
debug下
圖1
release下
圖2
總結,以上數據量比較大的情況下,memcpy效率>賦值操作,同時本人也測了數據量比較下的情況memcpy效率也很高,和賦值操作差不多。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章