Is there an optimal setting for the number of CPU cores exposed to a VM?
Solution 1:
It entirely depends on what the guest OS is doing. In my personal experience a VM is running a single program (build server, trac server, SVN, etc) so there is no reason to set it above one.
A possible case may be if it's an end user on say a University that is using the guest OS as a throwaway (i.e. TAs) then maybe set it to higher than one.
Solution 2:
The information in this answer is no longer correct. It was correct back in the days of strict co-scheduling, but is no longer the case.
I am keeping the answer below for posterity's sake, but please do not rely on it to be accurate for modern hypervisors.
You should never assign all your cores as vCPUs in a virtual machine.
If a VM requests any CPU time, VMWare must allocate either all the vCPUs or none of the vCPUs.
What this means is, on a 4-core system if you allocate all 4 cores, then only either the host, or the guest, can be allocated processor time, not both at the same time.
This will absolutely ruin performance in both environments.
Feel free to allocate an odd number of cores (say, 3). It may feel strange, but it's a totally legitimate option.
Generally, I never allocate more than n/2 vCPUs (in your case, 2), but I'm normally allocating cores on servers with between 12 and 32 cores where you can allocate many vCPUs without major impact.