我对C语言中各种数据类型的四则运算速度进行了测试。
操作系统:Windows 8.1 专业版 (64位)
编译器:GCC 4.8.1 64-bit Release (未开任何优化)
处理器:Intel Core i7-4702MQ
测试数据选择的都是12345和123
测试源程序:
#include<stdio.h> #include<time.h> #define test(expr){\ printf("test: %s 100000000 times\n",#expr);\ sum=0;\ for(j=0;j<10;j++){\ start=clock();\ for(i=0;i<1000000;i++){\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ expr;expr;expr;expr;expr;expr;expr;expr;expr;expr;\ }\ end=clock();\ t=(double)(end-start)/CLOCKS_PER_SEC;\ printf("%3d %.3fs",j+1,t);\ if(j%5==4)printf("\n");\ sum+=t;\ }\ printf("Avg %.3fs\n\n",sum/10);\ } int main(){ clock_t start,end; double t,sum; register int i,j; short short1=12345,short2=123,short3; long long1=12345,long2=123,long3; long long longlong1=12345,longlong2=123,longlong3; float float1=12345.0,float2=123.0,float3; double double1=12345.0,double2=123.0,double3; long double longdouble1=12345.0,longdouble2=123.0,longdouble3; test(NULL) test(short3=short1+short2) test(short3=short1-short2) test(short3=short1*short2) test(short3=short1/short2) test(short3=short1%short2) test(long3=long1+long2) test(long3=long1-long2) test(long3=long1*long2) test(long3=long1/long2) test(long3=long1%long2) test(longlong3=longlong1+longlong2) test(longlong3=longlong1-longlong2) test(longlong3=longlong1*longlong2) test(longlong3=longlong1/longlong2) test(longlong3=longlong1%longlong2) test(float3=float1+float2) test(float3=float1-float2) test(float3=float1*float2) test(float3=float1/float2) test(double3=double1+double2) test(double3=double1-double2) test(double3=double1*double2) test(double3=double1/double2) test(longdouble3=longdouble1+longdouble2) test(longdouble3=longdouble1-longdouble2) test(longdouble3=longdouble1*longdouble2) test(longdouble3=longdouble1/longdouble2) return 0; }
程序输出:
test: NULL 100000000 times 1 0.000s 2 0.000s 3 0.000s 4 0.000s 5 0.000s 6 0.000s 7 0.000s 8 0.000s 9 0.000s 10 0.000s Avg 0.000s test: short3=short1+short2 100000000 times 1 0.031s 2 0.047s 3 0.031s 4 0.031s 5 0.047s 6 0.031s 7 0.032s 8 0.046s 9 0.032s 10 0.031s Avg 0.036s test: short3=short1-short2 100000000 times 1 0.047s 2 0.047s 3 0.047s 4 0.078s 5 0.047s 6 0.046s 7 0.063s 8 0.047s 9 0.047s 10 0.046s Avg 0.052s test: short3=short1*short2 100000000 times 1 0.032s 2 0.047s 3 0.031s 4 0.047s 5 0.047s 6 0.062s 7 0.031s 8 0.047s 9 0.032s 10 0.031s Avg 0.041s test: short3=short1/short2 100000000 times 1 0.328s 2 0.422s 3 0.297s 4 0.390s 5 0.282s 6 0.343s 7 0.297s 8 0.375s 9 0.360s 10 0.375s Avg 0.347s test: short3=short1%short2 100000000 times 1 0.390s 2 0.281s 3 0.344s 4 0.281s 5 0.297s 6 0.282s 7 0.296s 8 0.282s 9 0.359s 10 0.313s Avg 0.313s test: long3=long1+long2 100000000 times 1 0.031s 2 0.047s 3 0.031s 4 0.031s 5 0.063s 6 0.047s 7 0.031s 8 0.047s 9 0.031s 10 0.031s Avg 0.039s test: long3=long1-long2 100000000 times 1 0.063s 2 0.047s 3 0.046s 4 0.047s 5 0.047s 6 0.047s 7 0.047s 8 0.047s 9 0.047s 10 0.047s Avg 0.048s test: long3=long1*long2 100000000 times 1 0.031s 2 0.047s 3 0.031s 4 0.031s 5 0.047s 6 0.031s 7 0.032s 8 0.031s 9 0.047s 10 0.031s Avg 0.036s test: long3=long1/long2 100000000 times 1 0.281s 2 0.313s 3 0.343s 4 0.313s 5 0.266s 6 0.281s 7 0.344s 8 0.328s 9 0.312s 10 0.281s Avg 0.306s test: long3=long1%long2 100000000 times 1 0.313s 2 0.281s 3 0.282s 4 0.343s 5 0.297s 6 0.438s 7 0.328s 8 0.281s 9 0.344s 10 0.281s Avg 0.319s test: longlong3=longlong1+longlong2 100000000 times 1 0.031s 2 0.063s 3 0.047s 4 0.031s 5 0.047s 6 0.031s 7 0.031s 8 0.047s 9 0.031s 10 0.032s Avg 0.039s test: longlong3=longlong1-longlong2 100000000 times 1 0.093s 2 0.047s 3 0.047s 4 0.047s 5 0.063s 6 0.109s 7 0.078s 8 0.047s 9 0.047s 10 0.047s Avg 0.063s test: longlong3=longlong1*longlong2 100000000 times 1 0.062s 2 0.047s 3 0.031s 4 0.032s 5 0.046s 6 0.032s 7 0.031s 8 0.031s 9 0.031s 10 0.047s Avg 0.039s test: longlong3=longlong1/longlong2 100000000 times 1 0.985s 2 0.953s 3 0.984s 4 0.891s 5 0.969s 6 0.906s 7 0.953s 8 1.062s 9 0.876s 10 0.921s Avg 0.950s test: longlong3=longlong1%longlong2 100000000 times 1 0.907s 2 1.015s 3 0.922s 4 1.031s 5 1.079s 6 0.937s 7 0.875s 8 0.938s 9 1.015s 10 1.000s Avg 0.972s test: float3=float1+float2 100000000 times 1 0.047s 2 0.031s 3 0.032s 4 0.031s 5 0.047s 6 0.031s 7 0.031s 8 0.047s 9 0.031s 10 0.063s Avg 0.039s test: float3=float1-float2 100000000 times 1 0.047s 2 0.031s 3 0.031s 4 0.031s 5 0.047s 6 0.032s 7 0.031s 8 0.031s 9 0.047s 10 0.062s Avg 0.039s test: float3=float1*float2 100000000 times 1 0.032s 2 0.031s 3 0.047s 4 0.078s 5 0.031s 6 0.047s 7 0.031s 8 0.032s 9 0.031s 10 0.047s Avg 0.041s test: float3=float1/float2 100000000 times 1 0.265s 2 0.266s 3 0.250s 4 0.250s 5 0.234s 6 0.250s 7 0.266s 8 0.234s 9 0.250s 10 0.266s Avg 0.253s test: double3=double1+double2 100000000 times 1 0.047s 2 0.031s 3 0.031s 4 0.047s 5 0.032s 6 0.031s 7 0.031s 8 0.047s 9 0.031s 10 0.031s Avg 0.036s test: double3=double1-double2 100000000 times 1 0.047s 2 0.032s 3 0.031s 4 0.031s 5 0.047s 6 0.031s 7 0.031s 8 0.032s 9 0.047s 10 0.031s Avg 0.036s test: double3=double1*double2 100000000 times 1 0.031s 2 0.031s 3 0.047s 4 0.031s 5 0.032s 6 0.047s 7 0.031s 8 0.031s 9 0.047s 10 0.094s Avg 0.042s test: double3=double1/double2 100000000 times 1 0.547s 2 0.500s 3 0.531s 4 0.719s 5 0.484s 6 0.484s 7 0.500s 8 0.500s 9 0.485s 10 0.531s Avg 0.528s test: longdouble3=longdouble1+longdouble2 100000000 times 1 0.250s 2 0.250s 3 0.250s 4 0.234s 5 0.250s 6 0.360s 7 0.234s 8 0.250s 9 0.328s 10 0.250s Avg 0.266s test: longdouble3=longdouble1-longdouble2 100000000 times 1 0.297s 2 0.250s 3 0.313s 4 0.234s 5 0.281s 6 0.250s 7 0.313s 8 0.265s 9 0.250s 10 0.282s Avg 0.273s test: longdouble3=longdouble1*longdouble2 100000000 times 1 0.250s 2 0.328s 3 0.281s 4 0.297s 5 0.281s 6 0.281s 7 0.266s 8 0.297s 9 0.281s 10 0.235s Avg 0.280s test: longdouble3=longdouble1/longdouble2 100000000 times 1 0.640s 2 0.735s 3 0.625s 4 0.640s 5 0.672s 6 0.625s 7 0.641s 8 0.656s 9 0.656s 10 0.688s Avg 0.658s
较为直观的统计表格(时间单位:毫秒):
sizeof | + | – | * | / | % | |
---|---|---|---|---|---|---|
short | 2 | 36 | 52 | 41 | 347 | 313 |
long | 4 | 39 | 48 | 36 | 306 | 319 |
long long | 8 | 39 | 63 | 39 | 950 | 972 |
float | 4 | 39 | 39 | 41 | 253 | |
double | 8 | 36 | 36 | 42 | 528 | |
long double | 16 | 266 | 273 | 280 | 658 |
结论: