寄存器变量速度比普通变量存取速度快。对于c程序,寄存器变量不能取地址,编译器会报错。对于c++程序,可以对寄存器变量进行取址操作,编译器不会报错,但是取出来的地址似乎不是寄存器地址,而是内存地址,不知道是不是c++编译器在涉及取址运算时将寄存器变量自动转换成普通变量来处理。
1、只有普通运算
对于上述的普通累加运算而言,采用普通变量耗时0.7177秒,采用寄存器变量耗时0.111秒,速度上确实有明显的差别。
2、涉及取址运算
如果涉及取址运算,采用普通变量耗时0.7867秒,采用寄存器变量耗时0.4792秒,速度上的差别就没有那么显著了。大家可以发现两种变量取出的地址分别是0x6ffe38和0x6ffe3c,是连续的两个地址,那都是内存地址。不能确定,是不是c++编译器在涉及取址运算时自动将寄存器变量当成普通变量来处理。
实际使用时,底层硬件环境的实际情况对寄存器变量的使用会有一些限制。每个函数中只有很少的变量可以保存在寄存器中,且只允许某些类型的变量。但是,过量的寄存器声明并没有什么害处,这是因为编译器可以忽略过量的或者不支持的寄存器变量声明。另外,无论寄存器变量实际上是不是存放在寄存器中,它的地址都是不能访问的。在不同的机器中,对寄存器变量的数目和类型的具体限制也是不同的。 ——《c程序设计语言(第二版) brain w.kernighan & dennis m.ritchie》
对于c程序,寄存器变量是不能取址的:
几种线程本地存储变量和普通变量的性能比较
god一直致力于研究高并发服务端的开发,这次要优化的是libgod库中的线程本地存储变量,线程本地存储变量访问非常频繁,优化后库的性能应该会提高不少。已知的线程本地存储方法有boost中的thread_specific_ptr类,gcc中的__thread关键字,pthread中的pthread_getspecific函数。这次测试这3中本地存储以及普通变量之间的性能差别,代码如下:
#include 《iostream》
#include 《stdio.h》
#include 《pthread.h》
#include 《boost/thread/thread.hpp》
#include 《boost/thread/tss.hpp》
using namespace std;
class c {
public:
c(int a) {
m_a = a;
printf(“c() %d\n”, m_a);
}
~c() {
printf(“~c() %d\n”, m_a);
}
private:
int m_a;
};
#define tm 3
#if tm == 1
boost::thread_specific_ptr《c》 pc;
const char *testtype = “boost”;
#elif tm == 2
__thread c *pc;
const char *testtype = “__thread”;
#elif tm == 3
pthread_key_t pc;
const char *testtype = “pthread”;
#else
c *pc;
const char *testtype = “normal”;
#endif
void boostthreadfunc() {
#if tm == 1
pc.reset(new c(10));
#elif tm == 2
pc = new c(20);
#elif tm == 3
if (pthread_key_create(&pc, null)) {
cout 《《 “pthread_key_create” 《《 endl;
return;
}
if (pthread_setspecific(pc, new c(30))) {
cout 《《 “pthread_setspecific” 《《 endl;
return;
}
#else
pc = new c(20);
#endif
int switches = 5000000;
int i = switches;
struct timeval tm_start, tm_end;
gettimeofday(&tm_start, null);
while (i--) {
#if tm == 1
c *c1 = pc.get();
c *c2 = pc.get();
c *c3 = pc.get();
c *c4 = pc.get();
c *c5 = pc.get();
c *c6 = pc.get();
c *c7 = pc.get();
c *c8 = pc.get();
c *c9 = pc.get();
c *c10 = pc.get();
c *c11 = pc.get();
c *c12 = pc.get();
c *c13 = pc.get();
c *c14 = pc.get();
c *c15 = pc.get();
c *c16 = pc.get();
c *c17 = pc.get();
c *c18 = pc.get();
c *c19 = pc.get();
c *c20 = pc.get();
c *c21 = pc.get();
c *c22 = pc.get();
c *c23 = pc.get();
c *c24 = pc.get();
c *c25 = pc.get();
c *c26 = pc.get();
c *c27 = pc.get();
c *c28 = pc.get();
c *c29 = pc.get();
c *c30 = pc.get();
c *c31 = pc.get();
c *c32 = pc.get();
c *c33 = pc.get();
c *c34 = pc.get();
c *c35 = pc.get();
c *c36 = pc.get();
c *c37 = pc.get();
c *c38 = pc.get();
c *c39 = pc.get();
c *c40 = pc.get();
#elif tm == 2
c *c1 = pc;
c *c2 = pc;
c *c3 = pc;
c *c4 = pc;
c *c5 = pc;
c *c6 = pc;
c *c7 = pc;
c *c8 = pc;
c *c9 = pc;
c *c10 = pc;
c *c11 = pc;
c *c12 = pc;
c *c13 = pc;
c *c14 = pc;
c *c15 = pc;
c *c16 = pc;
c *c17 = pc;
c *c18 = pc;
c *c19 = pc;
c *c20 = pc;
c *c21 = pc;
c *c22 = pc;
c *c23 = pc;
c *c24 = pc;
c *c25 = pc;
c *c26 = pc;
c *c27 = pc;
c *c28 = pc;
c *c29 = pc;
c *c30 = pc;
c *c31 = pc;
c *c32 = pc;
c *c33 = pc;
c *c34 = pc;
c *c35 = pc;
c *c36 = pc;
c *c37 = pc;
c *c38 = pc;
c *c39 = pc;
c *c40 = pc;
#elif tm == 3
c *c1 = (c *)pthread_getspecific(pc);
c *c2 = (c *)pthread_getspecific(pc);
c *c3 = (c *)pthread_getspecific(pc);
c *c4 = (c *)pthread_getspecific(pc);
c *c5 = (c *)pthread_getspecific(pc);
c *c6 = (c *)pthread_getspecific(pc);
c *c7 = (c *)pthread_getspecific(pc);
c *c8 = (c *)pthread_getspecific(pc);
c *c9 = (c *)pthread_getspecific(pc);
c *c10 = (c *)pthread_getspecific(pc);
c *c11 = (c *)pthread_getspecific(pc);
c *c12 = (c *)pthread_getspecific(pc);
c *c13 = (c *)pthread_getspecific(pc);
c *c14 = (c *)pthread_getspecific(pc);
c *c15 = (c *)pthread_getspecific(pc);
c *c16 = (c *)pthread_getspecific(pc);
c *c17 = (c *)pthread_getspecific(pc);
c *c18 = (c *)pthread_getspecific(pc);
c *c19 = (c *)pthread_getspecific(pc);
c *c20 = (c *)pthread_getspecific(pc);
c *c21 = (c *)pthread_getspecific(pc);
c *c22 = (c *)pthread_getspecific(pc);
c *c23 = (c *)pthread_getspecific(pc);
c *c24 = (c *)pthread_getspecific(pc);
c *c25 = (c *)pthread_getspecific(pc);
c *c26 = (c *)pthread_getspecific(pc);
c *c27 = (c *)pthread_getspecific(pc);
c *c28 = (c *)pthread_getspecific(pc);
c *c29 = (c *)pthread_getspecific(pc);
c *c30 = (c *)pthread_getspecific(pc);
c *c31 = (c *)pthread_getspecific(pc);
c *c32 = (c *)pthread_getspecific(pc);
c *c33 = (c *)pthread_getspecific(pc);
c *c34 = (c *)pthread_getspecific(pc);
c *c35 = (c *)pthread_getspecific(pc);
c *c36 = (c *)pthread_getspecific(pc);
c *c37 = (c *)pthread_getspecific(pc);
c *c38 = (c *)pthread_getspecific(pc);
c *c39 = (c *)pthread_getspecific(pc);
c *c40 = (c *)pthread_getspecific(pc);
#else
c *c1 = pc;
c *c2 = pc;
c *c3 = pc;
c *c4 = pc;
c *c5 = pc;
c *c6 = pc;
c *c7 = pc;
c *c8 = pc;
c *c9 = pc;
c *c10 = pc;
c *c11 = pc;
c *c12 = pc;
c *c13 = pc;
c *c14 = pc;
c *c15 = pc;
c *c16 = pc;
c *c17 = pc;
c *c18 = pc;
c *c19 = pc;
c *c20 = pc;
c *c21 = pc;
c *c22 = pc;
c *c23 = pc;
c *c24 = pc;
c *c25 = pc;
c *c26 = pc;
c *c27 = pc;
c *c28 = pc;
c *c29 = pc;
c *c30 = pc;
c *c31 = pc;
c *c32 = pc;
c *c33 = pc;
c *c34 = pc;
c *c35 = pc;
c *c36 = pc;
c *c37 = pc;
c *c38 = pc;
c *c39 = pc;
c *c40 = pc;
#endif
}
gettimeofday(&tm_end, null);
switches *= 40;
long long ns = (tm_end.tv_sec - tm_start.tv_sec) * 1000ll * 1000ll * 1000ll +
(tm_end.tv_usec - tm_start.tv_usec) * 1000ll;
std::cout 《《 “####benchmark result#### ” 《《 testtype 《《 std::endl;
std::cout 《《 “totol switches : ” 《《 switches 《《 std::endl;
std::cout 《《 “cost per switch(ns) : ” 《《 (double)ns/switches 《《 std::endl;
std::cout 《《 “all cost switch(ns) : ” 《《 ns 《《 std::endl;
std::cout 《《 “####benchmark result####” 《《 std::endl;
}
int main() {
boost::thread bt(&boostthreadfunc);
bt.join();
printf(“main exit.。\n”);
return 0;
}
Redis可视化工具ARDM介绍
数字资产之间的交易模式开发(币币交易系统)
可编程、小巧及高效零电压开关电源IC助USB PD设计推陈出新
欧司朗照明事业LEDVANCE正式完成股权交割
防水usb连接器的安全系数
普通变量与寄存器变量速度对比
一文解读内存管理单元 (MMU)
嵌入式linux的tftp配置
比亚迪证实将为特斯拉供应电池,目标车型可能是Model Y
微流控装置助力研究人员解密血流中的红细胞形状变化
混凝土搅拌站设备远程监控运维系统解决方案
浅谈模拟电路之电路零点
比亚迪实力演绎纯电动物流车进化历程
为什么说云正在改变制造业和分销商流程
区块链是如何让数据和加密扯上关系的
dfrobot20A电流传感器简介
ORB-SLAM2的变量命名规则代码运行流程简析
小米是如何成功逆转的?因为做对了这3点
ST推出新系列高精度LED驱动器STP16xPP05/STP
微型杀人机器就在我们身边!灭蚊灯有用吗?