高效c语言 内存拷贝. 测试结果 rand, loop, operator= % in x86-64 SUSE

If you  use C++,C, when a  memory copy  is needed.   Please  Use    memcpy.    That will be very  very  very  fast!!!!

if you are wondering who is better between :

1+(int)(255.0*rand()/(RAND_MAX+1.0));

1+rand()%255;

1 + g_rand_int(m_rand)%255;

if you want to know the operator "%" is single instruction or multi instruction.


Next,   please  see the test results.



GTK+ 2.0.     x86-64   SUSE linux


odie:~/Desktop/xudong/workhome_gtk/performance_test # ./PerformanceTest
Time 1byte copy    : 984.65ms used for 100000 loops. Each loop 9846.48ns
Time 4byte copy    : 369.03ms used for 100000 loops. Each loop 3690.32ns
Time 8byte copy    : 206.85ms used for 100000 loops. Each loop 2068.53ns
Time g_memmove()   : 32.19ms used for 100000 loops. Each loop 321.85ns
Time rand()        : 775.47ms used for 10000000 loops. Each loop 77.55ns
Time rand()simple  : 769.42ms used for 10000000 loops. Each loop 76.94ns
Time G_rand        : 614.43ms used for 10000000 loops. Each loop 61.44ns
Time "="           : 115.55ms used for 10000000 loops. Each loop 11.55ns
Time %%2           : 128.83ms used for 10000000 loops. Each loop 12.88ns
Time pure loop     : 98.91ms used for 10000000 loops. Each loop 9.89ns


you see .    the g_memmove   function is   32 times  faster  than     for loop



here is  my   source code.


#include <gtk/gtk.h>
#include <glib.h>
#include <stdlib.h>

#define MAX_SIZE       1024

void Comput_Print_Result(gchar *str,guint loops,GTimeVal tStart);

int main(int argc,char **argv)
{ 
	guchar buf111[MAX_SIZE];
	guchar buf222[MAX_SIZE];
	GTimeVal tstart;
	guint i,j;
	guint testTimes = 100000;
	


	//init data
	for(i=0;i<MAX_SIZE;i++)
	{
		buf111[i] = 1;
		buf222[i] = 2;
	}


//begin test:   1byte  copy
	g_get_current_time(&tstart);//start time
	for(j=0;j<testTimes;j++)
		for(i=0;i<MAX_SIZE;i++)
			buf111[i] = buf222[i];
	Comput_Print_Result("1byte copy    ",testTimes,tstart);


//test start:   4bytes  copy
	guint* srcInt = (guint*)buf222;
	guint* dstInt = (guint*)buf111;
	g_get_current_time(&tstart);//start time
	for(j=0;j<testTimes;j++)
		for(i=0;i<MAX_SIZE/4;i++)
			dstInt[i] = srcInt[i];
	Comput_Print_Result("4byte copy    ",testTimes,tstart);
                             

//test start:   8bytes  copy
	guint64* src64 = (guint64*)buf222;
	guint64* dst64 = (guint64*)buf111;
	g_get_current_time(&tstart);//start time
	for(j=0;j<testTimes;j++)
		for(i=0;i<MAX_SIZE/8;i++)
			dst64[i] = src64[i];
	Comput_Print_Result("8byte copy    ",testTimes,tstart);



	//init data
	for(i=0;i<MAX_SIZE;i++)
	{
		buf111[i] = 1;
		buf222[i] = 2;
	}


//test start:   g_memmove()
	g_get_current_time(&tstart);//start time
	for(j=0;j<testTimes;j++)
		g_memmove((void *)buf111,(void *)buf222, MAX_SIZE);
	Comput_Print_Result("g_memmove()   ",testTimes,tstart);

	//check data
	for(i=0;i<MAX_SIZE;i++)
		if(buf111[i] != buf222[i])
			g_print("mem copy failed\n");

	       




	GRand      *m_rand;
	m_rand = g_rand_new();
	guchar color;
	guint testTimes2 = 10000000;
//test start:   rand()
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++)
		color = 1+(int)(255.0*rand()/(RAND_MAX+1.0));
	Comput_Print_Result("rand()        ",testTimes2,tstart);

//test start:   rand() simple
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++)
		color = 1+rand()%255;
	Comput_Print_Result("rand()simple  ",testTimes2,tstart);

//test start:   Grand
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++)
		color = 1 + g_rand_int(m_rand)%255;
	Comput_Print_Result("G_rand        ",testTimes2,tstart);

//test start:   "="
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++)
		color = 2;
	Comput_Print_Result("\"=\"           ",testTimes2,tstart);

//test start:   "%"
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++)
		color = i%2;
	Comput_Print_Result("%%2           ",testTimes2,tstart);

//test start:   noting
	g_get_current_time(&tstart);//start time
	for(i=0;i<testTimes2;i++);
	Comput_Print_Result("pure loop     ",testTimes2,tstart);


	return 0;
}




void Comput_Print_Result(gchar *str,guint loops,GTimeVal tStart)
{
	GTimeVal tEnd;
	g_get_current_time(&tEnd);
	gfloat time_msec = (1000000.00*(tEnd.tv_sec - tStart.tv_sec)+ tEnd.tv_usec-tStart.tv_usec)/1000;
	gfloat time_each = time_msec / loops * 1000000;
	g_print("Time %s: %.2fms used for %d loops. Each loop %.2fns\n",str,time_msec,loops,time_each);
}



here is my  makefile


CC=gcc
PROG_NAME=PerformanceTest
INCS=
SRCS=PerformanceTest.c

#from xx.c to xx.o
OBJS=${SRCS:.c=.o}

#the libs for compiling GTK program
LIBS=gtk+-2.0 

#----------------do not edit this below
CFLAGS=`pkg-config --cflags ${LIBS}` -g -Wall
LDFLAGS=`pkg-config --libs ${LIBS}` -g -Wall
all: ${PROG_NAME}

${PROG_NAME}:${OBJS}
	${CC} -o ${PROG_NAME} ${OBJS} ${LDFLAGS}

${OBJS}:${INCS}

.c.o:
	${CC} -c {1}lt; ${CFLAGS}
clean:
	rm -f *.o ${PROG_NAME}
rebuild: clean all




你可能感兴趣的:(c,测试,语言,SuSE,each,X86)