当前位置:首页 » 编程软件 » 编译器优化if

编译器优化if

发布时间: 2022-10-23 19:11:55

1. shader中用for,if等条件语句为什么会使得帧率降低很多

1. For和If不一定意味着动态分支
在GPU上的分支语句(for,if-else,while),可以分为三类。
Branch的Condition仅依赖编译期常数
此时编译器可以直接摊平分支,或者展开(unloop)。对于For来说,会有个权衡,如果For的次数特别多,或者body内的代码特别长,可能就不展开了,因为会指令装载也是有限或者有耗费的
额外成本可以忽略不计
Branch的Condition仅依赖编译期常数和Uniform变量
一个运行期固定的跳转语句,可预测
同一个Warp内所有micro thread均执行相同分支
额外成本很低
Branch的Condition是动态的表达式
这才是真正的“动态分支”
会存在一个Warp的Micro Thread之间各自需要走不同分支的问题
2. 跳转本身的成本非常低
随着IP/EP(Instruction Pointer/Execution Pointer)的引入,现代GPU在执行指令上的行为,和CPU没什么两样。跳转仅仅是重新设置一个寄存器。
3.Micro Thread走不同分支时的处理
GPU本身的执行速度快,是因为它一条指令可以处理多个Micro Thread的数据(SIMD)。但是这需要多个Micro Thread同一时刻的指令是相同的。
如果不同,现代GPU通常的处理方法是,按照每个Micro Thread的不同需求多次执行分支。
x = tex.Load();
if(x == 5)
{
// Thread 1 & 2 使用这个路径
out.Color = float4(1, 1, 1, 1);
}
else
{
// Thread 3 & 4 使用这个路径
out.Color = float4(0, 0, 0, 0);
}
比如在上例中,两个分支的语句Shader Unit都会执行,只是不同的是如果在执行if分支,那么计算结果将不会写入到thread 3 和 4的存储中(无副作用)。
这样做就相当于运算量增加了不少,这是动态分支的主要成本。
但是如果所有的线程,都走的是同一分支,那么另外一个分支就不用走了。这个时候Shader Unit也不会去傻逼一样的执行另外一个根本不需要执行的分支。此时性能的损失也不多。并且,在实际的Shader中,除非特殊情况,大部分Warp内的线程,即便在动态分支的情况下,也多半走的是同一分支。
4. 动态分支和代码优化难度有相关性
这一点经常被忽视,就是有动态分支的代码,因为没准你要读写点什么,前后还可能有依赖,往往也难以被优化。比如说你非要闹这样的语句出来:
if(x == 1)
{
color = tex1.Load(coord);
}
else if(x == 2)
{
color = tex2.Load(coord);
}
...
你说编译器怎么给你优化。
说句题外话,为啥要有TextureArray呢?也是为了这个场合。TextureArray除了纹理不一样,无论格式、大小、坐标、LoD、偏移,都可以是相同的。这样甚至可以预见不同Texture Surface上取数据的内存延迟也是非常接近的。这样有很多的操作都可以合并成SIMD,就比多个Texture分别来取快得多了。这就是一个通过增加了约束(纹理格式、大小、寻址坐标)把SISD优化成SIMD的例子。

2. 为什么说用if(true==flag){}比if(flag==true){}更好

因为当你错把“if(true==flag)”写成“if(true=flag)”时你的程序编译是通不过的!而当你把“if(flag==true)”错写成“if(flag=true)”时你的编译器对程序的编译是可以通过的,所以一旦出了类似的错误则将很可能完全改变了你的程序的行为或目的.总之要记住:在c语言里是禁止对一个常量赋值的.这是培养自己成为一个优秀c程序员的基础.一旦有一天你开始在为win内核编程时,你就会发现这种编程习惯的重要性...

3. C语言中的条件赋值语句和if——else语句执行效率比较,哪一个效率高些,坐等高手解惑

一、条件语句和if else的效率一样
二、x++的效率比x=x+1要高,只是理论上,实际现在的机器性能很好,这个差异一般无法体现

综上所述,你上面的语句,用if else的效率要高。

4. 现代C/C++编译器有多智能

最近在搞C/C++代码的性能优化,发现很多时候自以为的优化其实编译器早就优化过了,得结合反汇编才能看出到底要做什么样的优化。
请熟悉编译器的同学结合操作系统和硬件谈一谈现代c/c++编译器到底有多智能吧。哪些书本上的优化方法其实早就过时了?
以及程序员做什么会让编译器能更好的自动优化代码?
举个栗子:
1,循环展开,大部分编译器设置flag后会自动展开;
2,顺序SIMD优化,大部分编译器设置flag后也会自动优化成SIMD指令;
3,减少中间变量,大部分编译器会自动优化掉中间变量;
etc.
查看代码对应的汇编:
Compiler Explorer
【以下解答】
举个之前看过的例子:
int calc_hash(signed char *s){ static const int N = 100003; int ret = 1; while (*s) { ret = ret * 131 + *s; ++ s; } ret %= N; if (ret < 0) ret += N; //注意这句 return ret;}
【以下解答】
举个简单例子,一到一百求和
#include int sum() { int ret= 0; int i; for(i = 1; i <= 100; i++) ret+=i; return ret;}int main() { printf("%d\n", sum()); return 0;}
【以下解答】
话题太大,码字花时间…
先放传送门好了。
请看Google的C++编译器组老大Chandler Carruth的演讲。这个演讲是从编译器研发工程师的角度出发,以Clang/LLVM编译C++为例,向一般C++程序员介绍理解编译器优化的思维模型。它讲解了C++编译器会做的一些常见优化,而不会深入到LLVM具体是如何实现这些优化的,所以即使不懂编译原理的C++程序员看这个演讲也不会有压力。
Understanding Compiler Optimization - Chandler Carruth - Opening Keynote Meeting C++ 2015
演示稿:https://meetingcpp.com/tl_files/mcpp/2015/talks/meetingcxx_2015-understanding_compiler_optimization_themed_.pdf
录像:https://www.youtube.com/watch?v=FnGCDLhaxKU(打不开请自备工具…)
Agner Fog写的优化手册也永远是值得参考的文档。其中的C++优化手册:
Optimizing software in C++ - An optimization guide for Windows, linux and Mac platforms - Agner Fog
要稍微深入一点的话,GCC和LLVM的文档其实都对各自的内部实现有不错的介绍。
GCC:GNU Compiler Collection (GCC) Internals
LLVM:LLVM’s Analysis and Transform Passes
========================================
反模式(anti-patterns)
1. 为了“优化”而减少源码中局部变量的个数
这可能是最没用的手工“优化”了。特别是遇到在高级语言中“不用临时变量来交换两个变量”这种场景的时候。
看另一个问题有感:有什么像a=a+b;b=a-b;a=a-b;这样的算法或者知识? - 编程
2. 为了“优化”而把应该传值的参数改为传引用
(待续…)
【以下解答】
推荐读一读这里的几个文档:
Software optimization resources. C++ and assembly. Windows, Linux, BSD, Mac OS X
其中第一篇:http://www.agner.org/optimize/optimizing_cpp.pdf
讲解了C++不同领域的优化思路和问题,还有编译器做了哪些优化,以及如何代码配合编译器优化。还有优化多线程、使用向量指令等的介绍,推荐看看。
感觉比较符合你的部分需求。
【以下解答】
一份比较老的slides:
http://www.fefe.de/source-code-optimization.pdf
【以下解答】
利用C++11的range-based for loop语法可以实现类似python里的range生成器,也就是实现一个range对象,使得
for(auto i : range(start, stop, step))
【以下解答】
我觉得都不用现代。。。。寄存器分配和指令调度最智能了
【以下解答】
每次编译poco库的时候我都觉得很为难GCC
【以下解答】
有些智能并不能保证代码变换前后语义是等价的
【以下解答】
诶诶,我错了各位,GCC是可以借助 SSE 的 xmm 寄存器进行优化的,经 @RednaxelaFX 才知道应该添加 -march=native 选项。我以前不了解 -march 选项,去研究下再来补充为什么加和不加区别这么大。
十分抱歉黑错了。。。以后再找别的点来黑。
误导大家了,实在抱歉。(??ˇ?ˇ??)
/*********以下是并不正确的原答案*********/
我是来黑 GCC的。
最近在搞编译器相关的活,编译OpenSSL的时候有一段这样的代码:
BN_ULONG a0,a1,a2,a3; // EmmetZC 注:BN_ULONG 其实就是 unsigned longa0=B[0]; a1=B[1]; a2=B[2]; a3=B[3];A[0]=a0; A[1]=a1; A[2]=a2; A[3]=a3;
【以下解答】
提示:找不到对象
【以下解答】
忍不住抖个机灵。
私以为正常写代码情况下编译器就能优化,才叫智能编译器。要程序员绞尽脑汁去考虑怎么写代码能让编译器更好优化,甚至降低了可读性,那就没有起到透明屏蔽的作用。
智能编译器应该是程序猿要较劲脑汁才能让编译器不优化。
理论上是这样的。折叠我吧。
【以下解答】
编译器智能到每次我都觉得自己很智障。
【以下解答】
虽然题主内容里是想问编译器代码性能优化方面的内容,但题目里既然说到编译器的的智能,我就偏一下方向来说吧。
有什么更能展示编译器的强大和智能?
自然是c++的模版元编程
template meta programming
简单解释的话就是写代码的代码,写的还是c++,但能让编译器在编译期间生成正常的c++代码。
没接触过的话,是不是听上去感觉就是宏替换的加强版?感觉不到它的强大呢?
只是简单用的话,效果上这样理解也没什么
但是一旦深入下去,尤其翻看大神写的东西,这明明看着就是c++的代码,但TM怎么完全看不懂他在干什么?后来才知道这其实完全是另外一个世界,可是明明是另外一个世界的东西但它又可以用来做很多正常c++能做的事....
什么?你说它好像不能做这个,不能做那个,好像做不了太多东西,错了,大错特错。就像你和高手考试都考了100分的故事一样,虽然分数一样,但你是努力努力再努力才得了满分,而高手只是因为卷面分只有100分.....在元编程面前,只有想不到,没有做不到。
再回头看看其他答案,编译器顺手帮你求个和,丢弃下无用代码,就已经被惊呼强大了,那模板元编程这种几乎能在编译期直接帮你“生成”包含复杂逻辑的c++代码,甚至还能间接“执行”一些复杂逻辑,这样的编译器是不是算怪兽级的强大?
一个编译器同时支持编译语法相似但结果不同却又关联的两种依赖语言,这个编译器有多强大多智能?
写的人思维都要转换几次,编译器转着圈嵌着套翻着番儿地编译代码的代码也肯定是无比蛋疼的,你说它有多强大多智能?
一个代码创造另外一个代码,自己能按照相似的规则生成自己,是不是听上去已经有人工智能的发展趋势了?
上帝说,要有光,于是有了光。
老子曰,一生二,二生三,三生万物。
信c++,得永生!
===
FBI WARNING:模板元编程虽然很强大,但也有不少缺点,尤其对于大型项目,为了你以及身边同事的身心健康,请务必适度且谨慎的使用。勿乱入坑,回头是岸。
【以下解答】
c++11的auto自动类型推断算么....
【以下解答】
智能到开不同级别的优化,程序行为会不同 2333
【以下解答】
这个取决于你的水平

5. c语言中纯粹的if语句耗时吗

要算机器周期的话,还取决于实际的软硬件环境的,
给你一个程序实验一下:
#include <stdio.h>

__forceinline unsigned _int64 My_clock(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
unsigned _int64 Start(void) { return My_clock();} // 开始时间
unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)
{return My_clock()-m_start - m_overhead; } // 停时间

void main()
{
unsigned _int64 m_start=0, m_overhead=0;
unsigned int N;
int a;
m_start = My_clock();
m_overhead = My_clock() - m_start - m_overhead;
m_start = My_clock(); // 正式开始
//测试程序段 Start
//这里写你要计算耗时的程序段!!!!!!!!!!!!!!!!!!!!!!!!
//测试程序段 End
N = Stop(m_start,m_overhead); // 获取耗时.
printf("Time:%I64d us\n", N);
}

我VC6+win7环境测试了一下:
#include <stdio.h>

__forceinline unsigned _int64 My_clock(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
unsigned _int64 Start(void) { return My_clock();} // 开始时间
unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)
{return My_clock()-m_start - m_overhead; } // 停时间

void main()
{
unsigned _int64 m_start=0, m_overhead=0;
unsigned int N;
int a;
m_start = My_clock();
m_overhead = My_clock() - m_start - m_overhead;
m_start = My_clock(); // 正式开始
//测试程序段 Start
if(a>7) a=1;
//测试程序段 End
N = Stop(m_start,m_overhead); // 获取耗时.
printf("Time:%I64d us\n", N);
}
耗时10us左右

下面这句呢?
if(a>7)
{
if(a>10)
{
if(a>15)
{
a=1;
}
}
}

#include <stdio.h>

__forceinline unsigned _int64 My_clock(void)
{
_asm _emit 0x0F
_asm _emit 0x31
}
unsigned _int64 Start(void) { return My_clock();} // 开始时间
unsigned _int64 Stop(unsigned _int64 m_start, unsigned _int64 m_overhead)
{return My_clock()-m_start - m_overhead; } // 停时间

void main()
{
unsigned _int64 m_start=0, m_overhead=0;
unsigned int N;
int a;
m_start = My_clock();
m_overhead = My_clock() - m_start - m_overhead;
m_start = My_clock(); // 正式开始
//测试程序段 Start
if(a>7)
{
if(a>10)
{
if(a>15)
{
a=1;
}
}
}
//测试程序段 End
N = Stop(m_start,m_overhead); // 获取耗时.
printf("Time:%I64d us\n", N);
}
耗时30us左右。

6. java 编译优化问题

java编译的结果是字节码而不是二进制,所以在运行时vm的优化才是重要的,包括VM的回收策略、分配给VM内存的大小都能在一定程度上影响性能。Sun的VM支持热点编译,对高频执行的代码段翻译的2进制会进行缓存,这也是VM的一种优化。

IBM JVM处理数学运算速度最快,BEA JVM处理大量线程和网络socket性能最好,而Sun JVM处理通常的商业逻辑性能最好。不过Hotspot的Server mode被报告有稳定性的问题。

Java 的最大优势不是体现在执行速度上,所以对Compiler的要求并不如c++那样高,代码级的优化还需要程序员本身的功底。

贴个java的运行参数:

Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print proct version and exit
-version:<value>
require the specified version to run
-showversion print proct version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see

java.lang.instrument

-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs rece use of OS signals by Java/VM (see

documentation)
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare:on require using shared class data, otherwise fail.

Java虚拟机(JVM)参数配置说明

在Java、J2EE大型应用中,JVM非标准参数的配置直接关系到整个系统的性能。
JVM非标准参数指的是JVM底层的一些配置参数,这些参数在一般开发中默认即可,不需

要任何配置。但是在生产环境中,为了提高性能,往往需要调整这些参数,以求系统达

到最佳新能。
另外这些参数的配置也是影响系统稳定性的一个重要因素,相信大多数Java开发人员都

见过“OutOfMemory”类型的错误。呵呵,这其中很可能就是JVM参数配置不当或者就没

有配置没意识到配置引起的。

为了说明这些参数,还需要说说JDK中的命令行工具一些知识做铺垫。

首先看如何获取这些命令配置信息说明:
假设你是windows平台,你安装了J2SDK,那么现在你从cmd控制台窗口进入J2SDK安装目

录下的bin目录,然后运行java命令,出现如下结果,这些就是包括java.exe工具的和

JVM的所有命令都在里面。

-----------------------------------------------------------------------
D:\j2sdk15\bin>java
Usage: java [-options] class [args...]
(to execute a class)
or java [-options] -jar jarfile [args...]
(to execute a jar file)

where options include:
-client to select the "client" VM
-server to select the "server" VM
-hotspot is a synonym for the "client" VM [deprecated]
The default VM is client.

-cp <class search path of directories and zip/jar files>
-classpath <class search path of directories and zip/jar files>
A ; separated list of directories, JAR archives,
and ZIP archives to search for class files.
-D<name>=<value>
set a system property
-verbose[:class|gc|jni]
enable verbose output
-version print proct version and exit
-version:<value>
require the specified version to run
-showversion print proct version and continue
-jre-restrict-search | -jre-no-restrict-search
include/exclude user private JREs in the version search
-? -help print this help message
-X print help on non-standard options
-ea[:<packagename>...|:<classname>]
-enableassertions[:<packagename>...|:<classname>]
enable assertions
-da[:<packagename>...|:<classname>]
-disableassertions[:<packagename>...|:<classname>]
disable assertions
-esa | -enablesystemassertions
enable system assertions
-dsa | -disablesystemassertions
disable system assertions
-agentlib:<libname>[=<options>]
load native agent library <libname>, e.g. -agentlib:hprof
see also, -agentlib:jdwp=help and -agentlib:hprof=help
-agentpath:<pathname>[=<options>]
load native agent library by full pathname
-javaagent:<jarpath>[=<options>]
load Java programming language agent, see

java.lang.instrument
-----------------------------------------------------------------------
在控制台输出信息中,有个-X(注意是大写)的命令,这个正是查看JVM配置参数的命

令。

其次,用java -X 命令查看JVM的配置说明:
运行后如下结果,这些就是配置JVM参数的秘密武器,这些信息都是英文的,为了方便

阅读,我根据自己的理解翻译成中文了(不准确的地方还请各位博友斧正)
-----------------------------------------------------------------------
D:\j2sdk15\bin>java -X
-Xmixed mixed mode execution (default)
-Xint interpreted mode execution only
-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
-Xnoclassgc disable class garbage collection
-Xincgc enable incremental garbage collection
-Xloggc:<file> log GC status to a file with time stamps
-Xbatch disable background compilation
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
-Xprof output cpu profiling data
-Xfuture enable strictest checks, anticipating future default
-Xrs rece use of OS signals by Java/VM (see

documentation)
-Xcheck:jni perform additional checks for JNI functions
-Xshare:off do not attempt to use shared class data
-Xshare:auto use shared class data if possible (default)
-Xshare:on require using shared class data, otherwise fail.

The -X options are non-standard and subject to change without notice.
-----------------------------------------------------------------------

JVM配置参数中文说明:
-----------------------------------------------------------------------
1、-Xmixed mixed mode execution (default)
混合模式执行

2、-Xint interpreted mode execution only
解释模式执行

3、-Xbootclasspath:<directories and zip/jar files separated by ;>
set search path for bootstrap classes and resources
设置zip/jar资源或者类(.class文件)存放目录路径

3、-Xbootclasspath/a:<directories and zip/jar files separated by ;>
append to end of bootstrap class path
追加zip/jar资源或者类(.class文件)存放目录路径

4、-Xbootclasspath/p:<directories and zip/jar files separated by ;>
prepend in front of bootstrap class path
预先加载zip/jar资源或者类(.class文件)存放目录路径

5、-Xnoclassgc disable class garbage collection
关闭类垃圾回收功能

6、-Xincgc enable incremental garbage collection
开启类的垃圾回收功能

7、-Xloggc:<file> log GC status to a file with time stamps
记录垃圾回日志到一个文件。

8、-Xbatch disable background compilation
关闭后台编译

9、-Xms<size> set initial Java heap size
设置JVM初始化堆内存大小

10、-Xmx<size> set maximum Java heap size
设置JVM最大的堆内存大小

11、-Xss<size> set java thread stack size
设置JVM栈内存大小

12、-Xprof output cpu profiling data
输入CPU概要表数据

13、-Xfuture enable strictest checks, anticipating future default
执行严格的代码检查,预测可能出现的情况

14、-Xrs rece use of OS signals by Java/VM (see

documentation)
通过JVM还原操作系统信号

15、-Xcheck:jni perform additional checks for JNI functions
对JNI函数执行检查

16、-Xshare:off do not attempt to use shared class data
尽可能不去使用共享类的数据

17、-Xshare:auto use shared class data if possible (default)
尽可能的使用共享类的数据

18、-Xshare:on require using shared class data, otherwise fail.
尽可能的使用共享类的数据,否则运行失败

The -X options are non-standard and subject to change without notice.

7. 在不考虑编译器优化等因素下,下面那个运算比较快

下面是在编译器不优化的情况下的代码反汇编后的结果
14: if (a > b)//我们看到,只有3条指令,
0040104D mov edx,dword ptr [ebp-4] //将a放入寄存器EDX
00401050 cmp edx,dword ptr [ebp-8] //比较EDX和b的大小
00401053 jle main+3Ch (0040105c) //如果a<b跳转到0040105c
15: {
16: return 1;
00401055 mov eax,1
0040105A jmp main+4Fh (0040106f)
17: }
18: if (a - b > 0)//我们看到有4条指令
0040105C mov eax,dword ptr [ebp-4]//将a放入EAX
0040105F sub eax,dword ptr [ebp-8]//a-b的差放入EAX
00401062 test eax,eax//比较EAX是否为0
00401064 jle main+4Dh (0040106d)//条件跳转
19: {
20: return 2;
00401066 mov eax,2
0040106B jmp main+4Fh (0040106f)
21: }
所以我认为if (a > b) {....}
效率更高

8. 编译器在编译阶段,究竟做哪些事情

1. 预处理首先源代码文件(.c/.cpp)和相关头文件(.h/.hpp)被预处理器cpp预编译成.i文件(C++为.ii)。预处理命令为:gcc –E hello.c –o hello.i预编译过程主要处理那些源代码中以#开始的预编译指令,主要处理规则如下:u 将所有的#define删除,并且展开所有的宏定义;u 处理所有条件编译指令,如#if,#ifdef等;u 处理#include预编译指令,将被包含的文件插入到该预编译指令的位置。该过程递归进行,及被包含的文件可能还包含其他文件。u 删除所有的注释//和 /**/;u 添加行号和文件标识,如#2 “hello.c” 2,以便于编译时编译器产生调试用的行号信息及用于编译时产生编译错误或警告时能够显示行号信息;u 保留所有的#pragma编译器指令,因为编译器须要使用它们。2. 编译编译过程就是把预处理完的文件进行一系列词法分析,语法分析,语义分析及优化后生成相应的汇编代码文件(.s)。编译的命令为:gcc –S hello.i –o hello.s或者从源文件直接输出汇编代码文件:gcc –S hello.c –o hello.s现在版本的GCC把预编译和编译两个步骤合并成一个步骤,由程序cc1来完成(C++为cc1plus)。3. 汇编汇编就是将汇编代码转变成机器可以执行的命令,生成目标文件(.o),汇编器as根据汇编指令和机器指令的对照表一一翻译即可完成。汇编的命令为:gcc –c hello.s –o hello.o或者从源文件直接输出目标文件:gcc –c hello.c –o hello.o4. 链接链接就是链接器ld将各个目标文件组装在一起,解决符号依赖,库依赖关系,并生成可执行文件。链接的命令为:ld –static crt1.o crti.o crtbeginT.o hello.o –start-group –lgcc –lgcc_eh –lc-end-group crtend.o crtn.o一般我们使用一条命令就可以完成上述4个步骤:gcc hello.c实际上gcc只是一些其它程序的包装,它会根据不同参数去调用预编译编译程序cc1、汇编器as、链接器ld。

9. 为什么说三目条件运算符:操作符能使编译器产生比if-then-else更优化的代码

所谓优化问题1.内存优化,就是所写代码的长度要尽可能的短小但又不是功能,占用内存尽可能的少; 2.计算机cpu执行编译好的代码所用的时间尽可能的少,因为计算机只有一个cpu,同一时间只能执行一个程序,这就要求我们写代码尽可能少的占用cpu的时间 根据以上的介绍我们再来谈三目运算符的?:与if -then-else优化的问题,三目运算符执行的起来使用最多是三个机械周期,而if -then-else至少是三个机械周期,这就是说它至少在占用cpu上是得到了优化,其次我们再来看看代码,很明显if -then-else的代码要比?:的代码写得要长一些,综合以上三目运算符?:是要比if -then-else更为的优化。在这里多说一点,三目运算符?:没有if -then-else使用的灵活,这也是他们的机构所决定的 越集中的东西越不灵活,无所谓谁更好,只有使用谁更适合

10. linux共享内存会不会被编译器优化

利用共享内存(Share Memory)可以让我们在任意两个进程之间传递数据,而且也是相对容易实现的一种方法,在正常情
况下,一个进程所使用的内存是不允许其它进程访问的,但是通过共享内存可以实现数据的共享。
使用共享内存用到的API函数有:

# include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

key_t ftok(const char *pathname, int proj_id)

其中pathname为当前进程的程序名,第二个参数为进程所开辟的第几个共享内存,返回一个key_t值,用于共享内存的获取
使用。

int shmget(key_t shmkey, int shmsiz, int flag)

void *shmat(int shmid, char *shmaddr, int shmflag)

int shmdt(char *shmaddr)

shmget是用来创建或者指向一块共享内存的函数,其中shmkey是共享内存的标识符,如果父子关系的进程间通信的话,这
个标识用IPC_PRIVATE替代,如果没有任何关系,可以使用ftok计算出来;Shmsiz是这块内存的大小;flag是这块内存的模
式和权限标识(IPC_CREAT, IPC_ALLOC, IPC_EXCL,0666权限)。函数成功返回共享内存的shmid,否则返回-1表示失败。

Shmat用来允许进程访问共享内存的函数,其中shmid是共享内存的ID,shmaddr是共享内存起始位置,shmflag是本进程对
内存操作模式,SHM_RDONLY是只读模式,成功返回共享内存的起始位置。

Shmdt与shmat相反,是用来禁止进程访问一块共享内存的函数。

int shmctl(int shmid, int cmd, struct shmid_ds *buf)

其中shmid是共享内存的ID,cmd是控制命令(IPC_STAT, IPC_SET, IPC_RMID),struct shmid_ds *buf是一个结构体指针
,如果要改变共享内存的状态,使用它来指定,获得状态就存放在这个结构体中。

上述API函数的详细说明请使用man(男人)查看:

# man shmget | shmat | shmdt | shmctl

下面使用一个简单的例子说明如何使用共享内存。

// sharememcut.c 拷贝用户输入的字符串到共享内存中

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

int main()

{

key_t shmkey;

int shmid, in_tmp;

char *head, *pos, in_data[4096], *in_ptr;

// Create share memory KEY

shmkey = ftok("sharememcut", 1);

// Get the share memory ID

shmid = shmget(shmkey, sizeof(in_data), IPC_CREAT | 0666);

// Allow the process to access share memory, and get the address

head = pos = shmat(shmid, 0, 0);

in_ptr = in_data;

// Receive the character from stdin, 'q' to quit

while ((in_tmp = getchar()) != 'q') {

*in_ptr = in_tmp;

in_ptr++;

}

*in_ptr = '\0';

in_ptr = in_data;

// Cut the data into share memory

while (*in_ptr != '\0') {

*pos++ = *in_ptr++;

}

// Prohabit the process to access share memory

shmdt(head);

return 0;

}

# gcc -o sharememcut sharememcut.c 编译sharememcut.c

# ./sharememcut 执行sharememcut创建共享内存



# ipcs -m | grep 4096 查看创建的共享内存

0x01068288 348848145 root 666 4096 0

// sharemempaste.c 将共享内存的内容显示在屏幕上并且删除共享内存

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

#include <stdio.h>

int main()

{

key_t shmkey;

int shmid;

char *head, *pos, out_data[4096], *out_ptr;

// Create share memory KEY

shmkey = ftok("sharememcut", 1);

// Get the share memory ID

shmid = shmget(shmkey, sizeof(out_data), 0666);

if (shmid == -1)

return -1;

// Allow the process to access share memory, and get the address

head = pos = shmat(shmid, 0, 0);

out_ptr = out_data;

// Get the data from share memory

while (*pos != '\0') {

*out_ptr++ = *pos++;

}

*out_ptr = '\0';

// Output the data into stdout

printf("%s\n", out_data);

fflush(stdout);

// Prohabit the process to access share memory

shmdt(head);

// Delete the share memory

shmctl(shmid, IPC_RMID, NULL);

return 0;

}

# gcc -o sharemempaste sharemempaste.c 编译sharemempaste.c

# ./sharemempaste 执行程序显示共享内存的内容

热点内容
游程编码c语言 发布:2025-05-16 21:26:51 浏览:585
帝来哪个配置值得购买 发布:2025-05-16 21:12:29 浏览:461
什么是nodejs前端服务器 发布:2025-05-16 21:12:17 浏览:404
编译选项立即绑定未定义符号 发布:2025-05-16 20:55:13 浏览:905
linuxmysql慢日志 发布:2025-05-16 20:47:58 浏览:270
村两委有哪些配置 发布:2025-05-16 20:34:47 浏览:292
我的世界有什么服务器好玩的 发布:2025-05-16 20:28:57 浏览:483
c语言按位与运算 发布:2025-05-16 20:24:10 浏览:754
苹果手机如何修改密码安全 发布:2025-05-16 20:23:34 浏览:193
图片文字识别算法 发布:2025-05-16 20:21:54 浏览:46