You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Shows which functions take the longest, but not what operations within the function take the longest. That takes looking into the function yourself and heavily leveraging Measure-Command
I downclock my laptop hard (1.06 GHz) to get more battery life out of it. But what ends up happening with PowerShell is that slow operations take a very long time to execute, making speed differences much more noticeable
Use preexisting values wherever possible
Google things a lot. Even if you think you know how to do something fast, there may be a faster or easier solution out there
Always investigate using a .NET library instead of a PowerShell command or CIM/WMI call, but don't assume that it will be faster
Initially tried (([System.Management.ManagementObjectSearcher]::new(([System.Management.ObjectQuery]::new("SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem")))).Get())
~10ms slower (30ms → 40ms)
Then tried $cimSession.GetInstance("root/cimv2","Win32_OperatingSystem").CimInstanceProperties
~18ms slower (30ms → 48.6)
Slower because it isn't grabbing specific properties, instead it is grabbing all of them
I later found $cimSession.EnumerateInstances("root/cimv2","Win32_OperatingSystem") which does the same thing but at about half the speed
After diving into how Get-CimInstance worked, I made $cimSession.QueryInstances("root/cimv2","WQL","SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem")
~10ms slower (30 → 40ms)
The weird thing with this one is that this should be almost exactly what Get-CimInstance uses, except that it uses CimSession.QueryInstancesAsync() instead
Using my new knowledge of how Get-CimInstance worked, I then attempted to optimize the execution of the command
First tried defining the flags -QueryDialect WQL -Namespace root/cimv2 in an attempt to prevent the function from needing to retrieve it
Ended up decreasing speed extremely slightly
Then tried replacing the flags -ClassName Win32_OperatingSystem -Property TotalVisibleMemorySize,FreePhysicalMemory with -Query "SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem" to prevent the code from generating the query
Caused a very slight speed increase (<1ms)
Then I got curious and checked what the speed is when I removed -CimSession $cimSession
Ended up saving ~1ms somehow
Might not show this speed gain in the script though, as the reuse of the CimSession might be what it's missing here
This will be where I keep track of the improvements to Winfetch I make in various pull requests
How I achieve my speed improvements:
$e.Name
instead of$e | Select-Object Name
)$null = operation
>[void]operation
>operation | Out-Null
$var | ForEach-Object
or$variable.foreach()
is faster[Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey()
is faster thanGet-ItemProperty
.Notes to self:
TODO:
Maybe manually scan all PS Package LocationsGet-CimInstance
(([System.Management.ManagementObjectSearcher]::new(([System.Management.ObjectQuery]::new("SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem")))).Get())
$cimSession.GetInstance("root/cimv2","Win32_OperatingSystem").CimInstanceProperties
$cimSession.EnumerateInstances("root/cimv2","Win32_OperatingSystem")
which does the same thing but at about half the speedGet-CimInstance
worked, I made$cimSession.QueryInstances("root/cimv2","WQL","SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem")
Get-CimInstance
uses, except that it usesCimSession.QueryInstancesAsync()
insteadGet-CimInstance
worked, I then attempted to optimize the execution of the command-QueryDialect WQL -Namespace root/cimv2
in an attempt to prevent the function from needing to retrieve it-ClassName Win32_OperatingSystem -Property TotalVisibleMemorySize,FreePhysicalMemory
with-Query "SELECT TotalVisibleMemorySize,FreePhysicalMemory FROM Win32_OperatingSystem"
to prevent the code from generating the query-CimSession $cimSession
Speed Improvement (Excluding Multithreading):
Before:
After (Subtracting Speed Gains):
The text was updated successfully, but these errors were encountered: