我的主要问题是,执行时间int和int8_t之间有什么区别吗?
在我正在研究的框架中,我经常阅读代码,其中一些参数被设置为int8_t函数,因为"该特定参数不能超出-126,125范围".
在很多地方,int8_t用于通信协议,或将数据包分成多个字段__attribute((packed)) struct.
但是在某些时候,它主要是因为有人认为使用与数据大小更匹配的类型会更好,可能会先考虑编译器.
鉴于代码是在Linux上运行,使用glibc使用gcc编译,并且内存或可移植性不是问题,我想知道它是否真的是一个好主意,性能方面.
我的第一印象来自于"试图比编译器更聪明的规则总是一个坏主意"(除非您知道需要优化的位置和方式).
但是,我不知道使用int8_t是否实际上是性能成本(更多测试和计算以匹配int8_t大小,需要更多操作来确保变量不会超出范围等),或者它确实提高了性能某种方式.
我不擅长阅读简单的asm,所以我没有将测试代码编译成asm以试图知道哪个更好.
我试图找到一个相关的问题,但所有讨论中,我发现int<size>_t与int约可移植性,而不是性能.
感谢您的输入.我们将非常感谢组装样品的解释或有关此问题的来源.
我试图通过实现一个包装器在C框架中使用C++库.我已经看到你需要在头文件中将函数声明为extern"C".然而,当我试图将我的动态库,名称重整没有被禁用,这会导致不确定的符号,当我尝试使用我的包装.
这是我的包装器的当前头,SbeOtfDecoder.h:
#ifndef ITF_SBEOTFDECODER_H_
#define ITF_SBEOTFDECODER_H_
// WARNING! In a C++ wrapper for C, any exception thrown by C++ code HAS TO BE CATCHED!
// Otherwise, it is "undefined behavior".
#ifdef __cplusplus
extern "C" {
#endif
///Create a new SbeOtfDecoder class instance, casted as void * for C code.
void *SbeOtfDecoder_new_with_file(char *filename);
///Destroy SbeOtfDecoder class instance.
void SbeOtfDecoder_free(void *self);
#ifdef __cpluscplus
} //extern "C"
#endif
#endif /* ITF_SBEOTFDECODER_H_ */
Run Code Online (Sandbox Code Playgroud)
和SbeOtfDecoder.cpp中的相应函数:
class SbeOtfDecoder
{
public:
SbeOtfDecoder(char *filename);
~SbeOtfDecoder();
};
SbeOtfDecoder::SbeOtfDecoder(char *filename) …Run Code Online (Sandbox Code Playgroud)