红宝石社区之外的人(以及一些内部人员)经常提到红宝石应用程序的速度很慢.对于我们中的许多人来说,这是一个无关紧要的,IO绑定操作等.但是,当它确实成为一个问题时,几乎没有什么可以阻止我们利用本机代码来加快速度.为此我想知道为什么RoR(本身是许多'慢'评论的目标)不利用任何本机元素加速自己?
有什么特别的原因吗?是否缺乏在代码库中优化的紧密循环?
有没有办法内联一些特定功能的选择性调用而不是全部?因为我知道的唯一形式是在开始时声明函数,并且应该影响对该函数的所有调用.
从我的课程讲师,他反复强调并要求我们不要使用"内联"关键字来实现功能.他说它不是编译器的"可移植"而不是"标准".考虑到这一点,是否存在允许"内联扩展"的"标准"替代方案?
c compiler-construction inline inline-code compiler-optimization
// common.h
// This is foo function. It has a body.
__inline void foo() { /* something */ }
// a.cpp
#include "common.h" // for foo function
// Call foo
// b.cpp
#include "common.h" // for foo function
// Call foo
Run Code Online (Sandbox Code Playgroud)
我想在我为发布版本构建时内联foo函数.我不想内联函数进行Debug构建.
我试过了,但链接器错误让我烦恼.
在这种情况下,foo函数的主体在common.h头文件中定义.
所以,如果我这样做
//common.h
#if !defined(_DEBUG)
__inline
#endif
void foo() { /* something */ }
Run Code Online (Sandbox Code Playgroud)
它将在DEBUG构建中遇到链接错误.因为两个模块试图包含common.h.
我不知道要解决它.
可能吗?
考虑下一个简单的例子:
标题:
// a.hpp
#ifndef A_HPP
#define A_HPP
#include <memory>
class A
{
public:
A();
int foo();
private:
struct Imp;
std::auto_ptr< Imp > pimpl;
};
#endif // A_HPP
Run Code Online (Sandbox Code Playgroud)
实施:
// a.cpp
#include "a.hpp"
struct A::Imp
{
int foo()
{
// do something and return the result
}
};
A::A() : pimpl( new Imp )
{}
int A::foo()
{
return pimpl->foo();
}
Run Code Online (Sandbox Code Playgroud)
主要的 :
// main.cpp
#include "header.hpp"
int main()
{
A a;
return a.foo();
}
Run Code Online (Sandbox Code Playgroud)
问题是:
方法是否A::Imp::foo会被内联到A::foo …
使用gcc v4.8.1
如果我做:
//func.hpp
#ifndef FUNC_HPP
#define FUNC_HPP
int func(int);
#endif
//func.cpp
#include "func.hpp"
int func(int x){
return 5*x+7;
}
//main.cpp
#include <iostream>
#include "func.hpp"
using std::cout;
using std::endl;
int main(){
cout<<func(5)<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
即使是简单的功能func也不会内联.无相结合inline,extern,static,并__attribute__((always_inline))在样机和/或定义改变了这种(这些说明的一些明显的组合,导致它甚至不会编译和/或产生警告,不谈论那些).我正在使用g++ *.cpp -O3 -o run和g++ *.cpp -O3 -S组装输出.当我看到装配输出时,我仍然看到call func.它似乎只有我能够正确内联函数的方法是拥有原型(可能没有必要)和头文件中函数的定义.如果标题只包含在整个程序中的一个文件中(仅main.cpp作为例子包含),它将编译并且函数将被正确内联,甚至不需要inline符.如果要将标头包含在多个文件中,则inline需要使用说明符来解决多个定义错误,这似乎是其唯一目的.该功能当然可以正确内联.
所以我的问题是:我做错了吗?我错过了什么吗?无论发生什么:
"编译器比你聪明.它知道什么时候应该比你更好地内联函数.永远不要使用C数组.总是使用std :: vector!"
- 每个其他StackOverflow用户
真?所以调用func(5)并打印结果比打印32更快?我会盲目地跟着你离开悬崖的边缘所有人都知道并且所有明智的gcc.
为了记录,上面的代码只是一个例子.我正在写一个光线跟踪器,当我将我的数学和其他实用程序类的所有代码移动到它们的头文件并使用说明inline符时,我看到了大量的性能提升.对于某些场景,字面意思快10倍.
我需要顶部标题有两个div内联 - 左右对齐.请检查代码.它是否正确?我在哪里需要添加div class ="clearfix"
<div class="container">
<div class="columns">
<div id="div-left">
Left aligned text
</div>
<div id="div-right">
Right aligned text
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
#div-left {
float:left;
display:inline
}
#div-right {
float:right;
display:inline
}
Run Code Online (Sandbox Code Playgroud) 有没有人知道是否有办法为InnoSetup源文件添加内联注释?
我做了一些实验:
; a comment (allowed)
[Setup]
AppName=MyApp // a comment (allowed)
AppVersion=1.2.3.4 ; a comment (allowed)
DefaultDirName="{pf}\My App" seems you can have anything at all here (allowed)
[Dirs]
Name: "{userdocs}\My App"
Name: {userdocs}\MyApp // a comment (allowed)
Name: "{userdocs}\My App" // a comment (not allowed)
Name: {userdocs}\MyApp ; // a comment (not allowed)
Name: "{userdocs}\My App" ; // a comment (not allowed)
Run Code Online (Sandbox Code Playgroud)
并且(我认为)我发现在编译器不期望任何更多操作数的任何地方都允许使用注释,但我更愿意使用更严格的语法(如果存在的话).
与像C++这样可以显式声明的语言不同inline,在Go中,编译器会动态检测内联的候选函数(C++也可以这样做,但Go不能同时执行这两种操作).还有一个调试选项可以看到可能的内联发生,但是很少有人在线记录go编译器执行此操作的确切逻辑.
假设我需要在每个n周期重新运行一组数据的大循环;
func Encrypt(password []byte) ([]byte, error) {
return bcrypt.GenerateFromPassword(password, 13)
}
for id, data := range someDataSet {
newPassword, _ := Encrypt([]byte("generatedSomething"))
data["password"] = newPassword
someSaveCall(id, data)
}
Run Code Online (Sandbox Code Playgroud)
目的是为了Encrypt正确地内联编译器需要考虑哪些逻辑?
我从C++中知道,通过引用传递将增加自动内联的可能性,而不使用显式inline关键字,但是要理解编译器确切地确定选择内联与否的决策是什么并不容易.像PHP这样的脚本语言会受到极大的影响,如果你做一个常量循环,addSomething($a, $b)其中基准测试如此十亿次循环它的成本与$a + $b(内联)几乎是荒谬的.
我看到这个例子,我有一个crc32.c文件,其中包含:
inline int32_t crc32_compute(int32_t *buffer, size_t size) {
...
}
Run Code Online (Sandbox Code Playgroud)
在头文件中我找到:
extern inline int32_t crc32_compute(int32_t *buffer, size_t size);
Run Code Online (Sandbox Code Playgroud)
对我来说,inline关键字没有任何效果,因为该函数应该在头文件上而不是在C文件上声明.这是正确的吗?
inline ×10
c ×4
c++ ×3
function ×2
c99 ×1
comments ×1
declaration ×1
g++ ×1
go ×1
html ×1
inline-code ×1
inno-setup ×1
native ×1
performance ×1
pimpl-idiom ×1
ruby ×1