Determine word size of my processor

2020-01-26 06:16发布

How do I determine the word size of my CPU? If I understand correct an int should be one word right? I'm not sure if I am correct.

So should just printing sizeof(int) would be enough to determine the word size of my processor?

8条回答
Luminary・发光体
2楼-- · 2020-01-26 07:03

Make a program that does some kind of integer operation many times, like an integer version of the SAXPY algorithm. Run it for different word sizes, from 8 to 64 bits (i.e. from char to long long).

Measure the time each version spends while running the algorithm. If there is one specific version that lasts noticeably less than the others, the word size used for that version is probably the native word size of your computer. On the other way, if there are several versions that last more or less the same time, pick up the one which has the greater word size.

Note that even with this technique you can get false data: your benchmark, compiled using Turbo C and running on a 80386 processor through DOS will report that the word size is 16 bits, just because the compiler doesn't use the 32-bit registers to perform integer aritmetic, but calls to internal functions that do the 32-bit version of each aritmetic operation.

查看更多
时光不老,我们不散
3楼-- · 2020-01-26 07:05

an int should be one word right?

As I understand it, that depends on the data size model. For an explanation for UNIX Systems, 64-bit and Data Size Neutrality. For example Linux 32-bit is ILP32, and Linux 64-bit is LP64. I am not sure about the difference across Window systems and versions, other than I believe all 32-bit Window systems are ILP32.

How do I determine the word size of my CPU?

That depends. Which version of C standard are you assuming. What platforms are we talking. Is this a compile or run time determination you're trying to make.

The C header file <limits.h> may defines WORD_BIT and/or __WORDSIZE.

查看更多
登录 后发表回答