我正在做一些关于制作内联函数的速度优势的研究.我没有这本书,但是我正在阅读的一篇文章暗示了进行函数调用需要相当大的开销; 当可执行文件的大小可以忽略不计,或者可以保留时,应该内联函数声明一个函数,以提高速度.
我已经编写了下面的代码来测试这个理论,从我所知道的,将内联函数声明为没有速度的好处.这两个函数在我的计算机上调用4294967295次时,在196秒内执行.
我的问题是,你对于为什么会这样做有什么想法?是现代编译器优化吗?是否缺乏功能中发生的大量计算?
任何有关此事的见解将不胜感激.在此先感谢朋友们.
#include < iostream >
#include < time.h >
// RESEARCH Jared Thomson 2010
////////////////////////////////////////////////////////////////////////////////
// Two functions that preform an identacle arbitrary floating point calculation
// one function is inline, the other is not.
double test(double a, double b, double c);
double inlineTest(double a, double b, double c);
double test(double a, double b, double c){
a = (3.1415 / 1.2345) / 4 + 5;
b = 9.999 / a + (a * a);
c = …Run Code Online (Sandbox Code Playgroud) 假设编译器实际上内联foo是否存在这两个语句之间的性能差异?
inline int foo (int val) {
return val;
}
int main () {
std::cout << foo(123) << std::endl;
std::cout << 123 << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
让我们忽略移动语义和复制省略可能带来的任何影响.
我有下面的JQuery代码,我需要清除这个动画完成的内联css
$(document).ready(function(){
$("#panel").hide(); //updated line, removing the #panel ID.
$('#tab2').toggle(function(){ //adding a toggle function to the #tab
$("#panel").show();
$('#ToolsTitleSpan').text("CLOSE TOOLS");
$('#tab2').stop().animate({right: '225'}, 800, 'swing', function() {});
$("#panel").stop().animate({right: '72', width: '230px', opacity:0.8}, 800, 'swing', function() {
$('.contentSideBar').fadeIn('slow');
});
},
function(){ //when the #tab is next cliked
$('#ToolsTitleSpan').text("OPEN TOOLS");
$('#tab2').stop().animate({right: '-=225'}, 800, 'swing', function() {
$('.contentSideBar').fadeOut(slow, function() {});
});
$('#panel').stop().animate({width:"0", position:'absolute', float: 'right', opacity:0.1}, 800, 'swing');
});
});
Run Code Online (Sandbox Code Playgroud)
如果需要,这是HTML代码:
<div id="panelKS"> <!--the hidden panel -->
<div class="contentSideBarKS">
</div>
</div>
<div id="tab2KS">
<span …Run Code Online (Sandbox Code Playgroud) 在每个示例中,我都看到关键字inline用于头文件中的公共类方法(或函数)。
在实现文件中内联私有类方法有什么特别的地方吗?
有人可以让我开始我需要做什么来实现Rcpp中的unlockEnvironment下面的代码?
碰到这个职位,并根据与C语言代码试图温斯顿畅的解决方案在线.它有效,但我觉得我对内联或C/C++知之甚少(实际上没什么,都是真的知道我在做什么;-)
所以我认为这将是一个很好的机会,最终开始学习如何使用R作为C和C++的接口.而且我想我会跳上Rcpp火车这样做!
require("inline")
inc <- '
/* This is taken from envir.c in the R 2.15.1 source
https://github.com/SurajGupta/r-source/blob/master/src/main/envir.c
*/
#define FRAME_LOCK_MASK (1<<14)
#define FRAME_IS_LOCKED(e) (ENVFLAGS(e) & FRAME_LOCK_MASK)
#define UNLOCK_FRAME(e) SET_ENVFLAGS(e, ENVFLAGS(e) & (~ FRAME_LOCK_MASK))
'
src <- '
if (TYPEOF(env) == NILSXP)
error("use of NULL environment is defunct");
if (TYPEOF(env) != ENVSXP)
error("not an environment");
UNLOCK_FRAME(env);
// Return TRUE if unlocked; FALSE otherwise
SEXP …Run Code Online (Sandbox Code Playgroud) 我有以下功能:
uint16_t foo(uint8_t input)
{
uint16_t N = 38;
if (!(input&1))
{
N = 0;
}
else
{
if ((input&2) >> 1)
{
N = ~N;
}
}
return N;
}
Run Code Online (Sandbox Code Playgroud)
我想有它改写没有ifs,就像其将内联函数38到任意0,38或65497给予input,并且仅使用标准C位变换操作.
关键不在于编译器可以内联函数,或者函数是快速的,而只是为了摆脱分支并且无论是什么都是恒定时间input.
第一个if很简单:
uint16_t c = ((input&1)^1)-1;
N &= c;
Run Code Online (Sandbox Code Playgroud)
但我找到一些简单的方法去做有条件的否定我遇到了麻烦.
当你想要返回一个闭包时,我理解嵌套函数的好处.但是当一个函数从未在它定义的函数之外使用时,是否需要保持嵌套的成本?
考虑:
let private validate number =
number > 100
let validateAndPrint (i : int) =
printfn "%i Greater than 100: %s" i ((validate i).ToString())
Run Code Online (Sandbox Code Playgroud)
与:
let validateAndPrint (i : int) =
let validate number =
number > 100
printfn "%i Greater than 100: %s" i ((validate i).ToString())
Run Code Online (Sandbox Code Playgroud)
这里的问题通常是当我有一些这些内部函数时,我只需要根据执行路径调用其中一个函数(通常不会禁止将这些函数保密或嵌套并测试排列).
validateAndPrint 也可以重写更多tdd-friendly以接受验证功能:
let validateAndPrint validate i = ...
Run Code Online (Sandbox Code Playgroud)
..但如果validation签名包含内部或私人类型,则考虑将所有内容公开并且参数列表爆炸.
任何重要的差异?
******尽管被标记为重复-另一个问题无法回答我的问题******
我想知道为什么不总是使用display:inline-block float:left的INSTEAD。内联块似乎在布局方面更容易控制,并且不存在必须清除浮点等问题。我试图弄清楚为什么使用一种而不是另一种。
非常感谢,
艾米丽
我想知道,内联小型私有函数是否是一个好主意?就我而言,这些函数仅出于可读性目的而存在,而且我知道,它们仅被调用过几次,因此较大的字节码大小无关紧要。
我知道,性能提升也可能是微不足道的,因为我没有传递函数类型(编译器实际上警告我),但是让我们假设它是我们应用程序中的热点。
实际示例:
我有一个理论上无限的符号带(像图灵机一样),它由两个阵列(分别是位置<0和位置> = 0的左侧和右侧)建模。现在,我已经进行了许多读写操作,这些操作被假定为很多东西。
在Java中,我有:
/**
* @param cell the cell
* @return the symbol at the specified cell
*/
public char read(int cell) {
char[] tape;
if (cell < 0) {
tape = left;
cell = -cell - 1;
} else {
tape = right;
}
return cell < tape.length ? tape[cell] : blank;
}
/**
* Writes a symbol to the specified cell.
* @param c the symbol
* @param cell the cell
*/ …Run Code Online (Sandbox Code Playgroud)