分页: 1 / 1

c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-08 20:53
weiweishuo
下面的代码测试了int转换为float的开销:
代码里用到了一个【RUN--ENDRUN】宏,不要奇怪没见过,是我自己写的。

代码: 全选

#include"../include/run.h"
void main(void){
    float b,b1,b2,b3,b4;
    int c,c1,c2,c3,c4;
        //RUN和ENDRUN之间的代码会重复执行100000000*9次,每执行一亿次,记录下执行时间,重复9次,取均值
    RUN(100000000,9)//【RUN--ENDRUN】是测试性能的宏,后面贴了源码
    /**b=(float)(exec_count);   
    b1=(float)(exec_count);
    b2=(float)(exec_count);
    b3=(float)(exec_count);
    b4=(float)(exec_count);*/
        c=exec_count;
    c1=exec_count;
    c2=exec_count;
    c3=exec_count;
    c4=exec_count;
    ENDRUN
}
gcc -o t t.c -std=c99
./t运行,输出:
332 325 326 326 326 326 325 326 327
average:326
以上是没有类型转换的性能评价,再把注释部分对调,看类型转换的情况下,输出:
723 710 712 711 709 712 713 710 711
average:712
---------------------------------------------
测试环境ubuntu12.1,gcc4.7,联想y470
---------------------------------------------


之所以做这样的测试,是在读《3D游戏编程大师》时候,看到作者说“这样....可以避免浮点数和整数之间的转换,因为作者不信任编译器"。
在下是新手,请大侠们说说上面的性能差距意味着什么,可以忽略吗?当然也欢迎指出测试代码的不严谨处,因为我对cache,编译优化之类不太懂。
附:run.h

代码: 全选

#ifndef RUN_H
#define RUN_H
#include<sys/time.h>
#include<stdio.h>
struct timeval start,end;//global vars,remember not to use repeatly
int var_run_num,run_count,exec_count,timeCost,timeCost_total;//look up
#define RUN(exec_num,run_num)\
    timeCost_total=0;\
    var_run_num=run_num;\
    printf("run start-----------\nexec_strength:%d\n",exec_num);\
    for(run_count=0;run_count<run_num;run_count++){\
        gettimeofday(&start,NULL);\
        for(exec_count=0;exec_count<exec_num;exec_count++){
#define ENDRUN\
        }\
        gettimeofday(&end,NULL);\
        timeCost=(end.tv_sec-start.tv_sec)*1000+(end.tv_usec-start.tv_usec)/1000;\
        timeCost_total+=timeCost;\
        printf("%d  ",timeCost);\
    }\
    printf("\naverage:%d\nrun exit-------------\n",timeCost_total/var_run_num);   
#endif

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-08 22:35
eexpress
尽量选择适合的数据类型。尽量少转换。其他不知了。

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-09 12:11
huangbster
int和float在内存中储存的格式不一样,float存储格式使用的是IEEE标准,因此float和int这间的每次转换都要做一次浮点运算,效率可想而知。
例子1:
d=(int)a;//float转int
汇编:
flds 32(%esp)
fnstcw 30(%esp)
movzwl 30(%esp), %eax
movb $12, %ah
movw %ax, 28(%esp)
fldcw 28(%esp)
fistpl 40(%esp)
fldcw 30(%esp)

例子2:
b=(float)c;//int转float
汇编:
fildl 36(%esp)
fstps 44(%esp)

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-09 22:22
weiweishuo
huangbster 写了:int和float在内存中储存的格式不一样,float存储格式使用的是IEEE标准,因此float和int这间的每次转换都要做一次浮点运算,效率可想而知。
例子1:
d=(int)a;//float转int
汇编:
flds 32(%esp)
fnstcw 30(%esp)
movzwl 30(%esp), %eax
movb $12, %ah
movw %ax, 28(%esp)
fldcw 28(%esp)
fistpl 40(%esp)
fldcw 30(%esp)

例子2:
b=(float)c;//int转float
汇编:
fildl 36(%esp)
fstps 44(%esp)
恩,我想知道这样的开销意味着什么,算不算大呢?

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-09 22:34
cuihao
意味着什么?看你是写什么、怎么写了。

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-11 10:27
sanz
这也算“新手论坛”? :em20

Re: c语言的类型转换有多慢【欢迎讨论】

发表于 : 2013-03-11 11:13
huangbster
weiweishuo 写了:
huangbster 写了:int和float在内存中储存的格式不一样,float存储格式使用的是IEEE标准,因此float和int这间的每次转换都要做一次浮点运算,效率可想而知。
例子1:
d=(int)a;//float转int
汇编:
flds 32(%esp)
fnstcw 30(%esp)
movzwl 30(%esp), %eax
movb $12, %ah
movw %ax, 28(%esp)
fldcw 28(%esp)
fistpl 40(%esp)
fldcw 30(%esp)

例子2:
b=(float)c;//int转float
汇编:
fildl 36(%esp)
fstps 44(%esp)
恩,我想知道这样的开销意味着什么,算不算大呢?
意味着如果选择的类型不对,那么程序就要频繁做类型转换,程序的效率就会降低。为此,如果想程序高效,那么应该尽量减少类型转换。