Monday, August 29, 2011 12:44 PM
The book I am reading says in C# int is 32-bit. Is it 32-bit on all CPUs (32 bit cpu, 64 bii cpu)?. How is cpu related to size of variables?. Why people say compiling to a specific CPU improves performance?.
Monday, August 29, 2011 12:55 PM
int is always 32bit in C#.
internally, the references are using 32bit or 64bit, depending, what kind of process/CPU you have. That means, that structures/classes can be different in size, depending on the process on which your C#-program is running. BUT an int variable is always 32 bit.
Monday, August 29, 2011 1:39 PM
Why people say compiling to a specific CPU improves performance?.
Where did you saw this ? AFAIK it was true when we changed from 16 bit to 32 bit mode as we had a new instruction set.
This is AFAIK not true for 64 bit. My understanding is that primary benefit is to be able to address much more memory than previously possible (which coukld marginaly improve performance but shouldn't be considered as a goal, the goal is being able to write an app able to easily address much more memory).
Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
Monday, August 29, 2011 1:59 PM
The reference types (object references and pointers) are the size of a memory address, which would be 32 bits (4 bytes) on a 32-bit platform, and 64-bits (8 bytes) on a 64-bit platform. All the other data types are defined by the language, not the CPU or OS. See the reference chart below.
0 to 255
-128 to 127
-2,147,483,648 to 2,147,483,647
0 to 4294967295
-32,768 to 32,767
0 to 65535
-9223372036854775808 to 9223372036854775807
0 to 18446744073709551615
Single-precision floating point type
-3.402823e38 to 3.402823e38
Double-precision floating point type
-1.79769313486232e308 to 1.79769313486232e308
A single Unicode character
Unicode symbols used in text
Logical Boolean type
True or false
Base type of all other types
A sequence of characters
Precise fractional or integral type that can represent decimal numbers with 29 significant digits
±1.0 × 10e−28 to ±7.9 × 10e28
Monday, August 29, 2011 3:16 PM
Is C# int 32-bit on all CPUs?Yes.
Why people say compiling to a specific CPU improves performance?
In C#, there is no such thing as compiling to a specific CPU. The code is always compiled to IL (intermediate language). Therefore, the EXE file contains mostly IL code, and only a tiny section of native code for the initialization of CLR. When the EXE file is loaded in memory, IL code is compiled to platform-specific native code on-the-fly. This is called just-in-time compilation (JIT).
JIT compilation is performed every time a new IL module is loaded in memory. The time delay required for JIT is generally unnoticeable. However if you want to get rid of this delay, you can pre-generate native code translations of your managed assemblies using NGEN. NGEN is a command-line utility that generates and caches the native image of your managed assemblies.
The native image generated by NGEN is specific to your CPU.
P.S.: The compiler has a "target platform" switch, which can be set to x86, Itanium, x64, or anycpu. Don't be misled by this switch. You can use this switch to prevent your application from running on platforms you don't support. You may need this switch if you have unmanaged DLL dependencies. This switch does not generate native code for a specific CPU.
I hope this information helps you...
Monday, August 29, 2011 9:06 PMThanks everyone for the response. All of the responses were helpful to me.
Monday, August 29, 2011 9:49 PM
I have a follow-up question on this. There is a compile option in Visual Studio to select "AnyCPU". Is there a reason, c# program is compiled to a specific CPU?.Other options include "x86" and "64bit"
Monday, August 29, 2011 10:10 PM
For example if your assembly depends on a 64bit DLL file, then you have to select 64bit. This way, your application will refuse to run on x86 platforms.
But, you will get exactly the same IL code if you select 64bit or x86. Selecting 64bit or x86 is not about generating an EXE optimized for a specific platform, it is only about checking the environment at the beginning of the execution.
Unless you have unmanaged dependencies that are known to cause problems on some platforms, best-practice is leaving this option anycpu.
Tuesday, August 30, 2011 12:10 PM
If your assembly depends on a 64bit DLL file, then you have to select 64bit. This way, your application will refuse to run on x86 platforms.
And if your application depends on a 32bit DLL file, you have to select x86. That way your application will run in 32bit mode on 64bit platforms.
Wednesday, August 31, 2011 3:21 AMThe dependent DLL files that we are talking here, are these .net run time dlls?. I googled for .net runtime versions for 32bit and 64 bit. Infact they have separate .net runtimes for 32 and 64 bits.
Wednesday, August 31, 2011 12:44 PM
No.. usually these dependend DLL are COM-DLLs. Often programmed in C++.
These C++ DLL are platform/processor dependend, which means: you have to start your process/program with the platform which these DLLs have.
Wednesday, August 31, 2011 2:10 PM
Could be any dll compiled specifically for 32bit or 64 bit. If your application is compiled for AnyCPU or 64bit and you try loading a 32bit dll, you're in trouble.
You can also have your code dynamically load a dll depending on which platform you're working. I once made an ApiFactory class able to select which dll to use for p/invoking.