One of my friends works for the company doing video processing. One, he told me that the company code runs slowly on a 64-bit processor than on 32. It can be a number of reasons. I would like to talk about one most important one.
One of the problems was the size of the integers.
When your code runs on the 32-bit processor you were using int for all integer numbers. When you compile the same code on a 64-bit processor, the integer stays 32 bit.
NOTE: The size of int variable in C++ stays 32 bit when code is compiled for 64bit processor or for 32 bit.
So, when you have the following code:
unsigned char * data = new char[n];
int i = 10;
data[i] = 'a';
When the code is compiled and executed on a 64-bit processor, the memory addressing is 64 bit too. As a result when doing simple and quick operation like data[i] which equals to offset_to_data+I is converted to offset_to_data + convert_to_64(i). The int I variable needs to be converted to 64 bit.
As a result, when working with memory all the int variables are converted to 64 bit. And eventually, additional code is executed each time you access memory!
The solution is very simple. You need to convert your code to use ssize_t type variable instead of integers and use size_t type variable instead of unsigned int.
When the code is compiled on the 32-bit processor the ssize_t type variable has the size of 32 bit.
When code is compiled on a 64-bit processor, the ssize_t type variable has the size of 64 bit.
One issue discovered when working with code that must compile on Windows and Linux. The problem is with printing these variables. Somehow Linux and Windows compilers use different standards when printing these numbers.
Here is a solution:
#define SIZE_T_FORMAT "%Iu"
#define SSIZE_T_FORMAT "%Id"
#define SIZE_T_FORMAT "%zd"
#define SSIZE_T_FORMAT "%zu"
ssize_t num = 1234;
printf("ssize_t number: " SIZE_T_FORMAT "\n", num);
Feel free to leave comments/questions here.
About the author
For the past 15 years I’ve been leading the evolution of startups and enterprises to achieve the highest level of security and compliance. Throughout my career I’ve been a Cyber Security expert and advanced solutions architect with many years of hands on experience both on offensive and defensive side. Knowledgeable at the highest level in application development, networking, data and databases, web applications, large scale Software as a Service solutions, cloud security and blockchain technologies.
I’ve been working with CISO’s of international enterprises, helping them set Information Security strategy, and overseeing the implementation of these recommendations. As part of these projects, I’ve been assisting companies to achieve compliance in GDPR, PCI, HIPAA and SOX.
Among my credits, I was a founder of a database security company GreenSQL/Hexatier which was acquired by Huawei and I’ve co-founded Kesem.io, Secure multi-signature Crypto wallet.
Specialties: Software and cloud architecture, Compliance (GDPR, HIPAA, PCI, SOX), blockchain technologies, software development, secure architectures, project management and low level research.