当前位置:网站首页>Efficient C language memory copy Test result Rand, loop, operator =% in x86-64 SUSE
Efficient C language memory copy Test result Rand, loop, operator =% in x86-64 SUSE
2022-04-21 20:03:00 【delacrxoix_ xu】
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
版权声明
本文为[delacrxoix_ xu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212001343650.html
边栏推荐
- What is annotation
- glew, glee与 gl glu glut glx glext的区别和关系
- VIM from dislike to dependence (6) -- insertion mode
- 长安深蓝C385产品信息曝光 瞄准20万级别,头号目标Model 3!
- Building / building projects on the command line using cmake
- 有趣的灵魂千篇一律,电脑滑动关机,仅需2步
- How to restore the deleted photos? Four schemes, which is the official guide
- CUDA02 - 访存优化和Unified Memory
- Jerry's switch interrupt function using unshielded interrupt [chapter]
- Ali IOT
猜你喜欢

Ali IOT

Xinguan is merciless, human beings have feelings, Xinlong agriculture ensures people's livelihood and jointly fights the epidemic -- condolences to the front line of fighting the epidemic, love the el

Digital business cloud: analyze the current situation of enterprise procurement management and promote the optimization and upgrading of enterprise procurement mode

高端制造業企業信息化解决方案,工業電商平臺設備、數據、體系預測性維護

Three implementation methods of quick sorting

高端制造业企业信息化解决方案,工业电商平台设备、数据、体系预测性维护

图像中stride的含义

DolphinDB VSCode 插件使用教程

Enterprise cross-border e-commerce platform service solution, cross-border e-commerce trade business framework construction, operation and maintenance

How does the Mui tab realize circular rotation
随机推荐
【verbs】使用ibverbs api注意事项|libibverbs 中 fork() 支持的状态如何?
[verbs] precautions for using ibverbs API | what is the status of fork() support in libibverbs?
MFC CCombobox usage example
如何判断Int型值的第nbit位是否是1还是0
High end manufacturing enterprise informatization solution, predictive maintenance of industrial e-commerce platform equipment, data and system
mui选项卡怎么实现循环轮播
Install mysql, MSSQL, Oracle, mongdb and redis collections in docker
How to solve the thread safety problem without locking
Surface point cloud normal
数商云:剖析企业采购管理的现状,推进企业采购模式优化升级
Registration for PMP examination starts in the second half of 2022. Notice
Comment déterminer si le BIT nbit de la valeur de type int est 1 ou 0
渤海期货开户安全吗?渤海期货公司开户方法是什么?
长安深蓝C385产品信息曝光 瞄准20万级别,头号目标Model 3!
Building / building reusable QML modules using cmake
06.适配器模式
Jerry uses hardware timer to simulate interrupt request [chapter]
界面组件Telerik UI for WPF入门指南 - 颜色主题生成器
[maximum value of jz47 gift]
高效c语言 内存拷贝. 测试结果 rand, loop, operator= % in x86-64 SUSE