CPUID implementations in C++

Accessing raw CPUID information is actually very easy, here is a C++ class for that which works in Windows, Linux and OSX: #ifndef CPUID_H #define CPUID_H #ifdef _WIN32 #include <limits.h> #include <intrin.h> typedef unsigned __int32 uint32_t; #else #include <stdint.h> #endif class CPUID { uint32_t regs[4]; public: explicit CPUID(unsigned i) { #ifdef _WIN32 __cpuid((int *)regs, (int)i); … Read more

Which is faster: x

Potentially depends on the CPU. However, all modern CPUs (x86, ARM) use a “barrel shifter” — a hardware module specifically designed to perform arbitrary shifts in constant time. So the bottom line is… no. No difference.

Detecting the number of processors

System.Environment.ProcessorCount returns the number of logical processors http://msdn.microsoft.com/en-us/library/system.environment.processorcount.aspx For physical processor count you’d probably need to use WMI – the following metadata is supported in XP/Win2k3 upwards (Functionality enabled in SP’s prior to Vista/Win2k8). Win32_ComputerSystem.NumberOfProcessors returns physical count Win32_ComputerSystem.NumberOfLogicalProcessors returns logical (duh!) Be cautious that HyperThreaded CPUs appear identical to multicore’d CPU’s yet the performance … Read more

Accurate calculation of CPU usage given in percentage in Linux?

According the htop source code, my assumptions looks like they are valid: (see static inline double LinuxProcessList_scanCPUTime(LinuxProcessList* this) function at LinuxProcessList.c) // Guest time is already accounted in usertime usertime = usertime – guest; # As you see here, it subtracts guest from user time nicetime = nicetime – guestnice; # and guest_nice from nice … Read more

Difference between core and processor

A core is usually the basic computation unit of the CPU – it can run a single program context (or multiple ones if it supports hardware threads such as hyperthreading on Intel CPUs), maintaining the correct program state, registers, and correct execution order, and performing the operations through ALUs. For optimization purposes, a core can … Read more