我能得到最好的性能,从而使静态变量? [英] Can I get best performance making static variables?

查看:99
本文介绍了我能得到最好的性能,从而使静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么有些人做的:

char baa(int x) {
    static char foo[] = " .. ";
    return foo[x ..];
}

而不是:

char baa(int x) {
    char foo[] = " .. ";
    return foo[x ..];
}

看起来像Linux的源$ C ​​$ CS应用非常普遍。有性能差异?如果是,有人可以解释,为什么?先谢谢了。

looks like very common on linux source codes applications. There performance difference? if yes, can someone explain why? Thanks in advance.

推荐答案

这不是对性能的本身的,而是以减少内存使用情况。有的性能提升,但它不是(通常)的首要原因,你会看到code类的。

It's not for performance per se, but rather to decrease memory usage. There is a performance boost, but it's not (usually) the primary reason you'd see code like that.

在一个函数变量是在栈上分配的,他们会被保留,并且每个函数被调用时删除,重要的是,他们将朝着堆栈大小限制这是一个严重制约计数许多嵌入式和资源约束平台。

Variables in a function are allocated on the stack, they'll be reserved and removed each time the function is called, and importantly, they will count towards the stack size limit which is a serious constraint on many embedded and resource-constrained platforms.

但是,静态变量存储在任 .BSS .DATA 段(非明确初始化静态变量会去 .BSS ,静态初始化静态变量会去 .DATA ),堆栈。编译器还可以利用这一点来执行某些优化。

However, static variables are stored in either the .BSS or .DATA segment (non-explicitly-initialized static variables will go to .BSS, statically-initialized static variables will go to .DATA), off the stack. The compiler can also take advantage of this to perform certain optimizations.

这篇关于我能得到最好的性能,从而使静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆