https://github.com/deadlydog/PowerShell.Experiment.ClassPerformanceComparison
A test repo to compare the performance of the different ways to define classes in PowerShell
https://github.com/deadlydog/PowerShell.Experiment.ClassPerformanceComparison
Last synced: 2 months ago
JSON representation
A test repo to compare the performance of the different ways to define classes in PowerShell
- Host: GitHub
- URL: https://github.com/deadlydog/PowerShell.Experiment.ClassPerformanceComparison
- Owner: deadlydog
- License: mit
- Created: 2023-09-19T22:36:29.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-12T22:22:29.000Z (over 1 year ago)
- Last Synced: 2024-12-04T01:33:55.654Z (10 months ago)
- Language: PowerShell
- Size: 30.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: ReadMe.md
- Funding: .github/FUNDING.yml
- License: License.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
- jimsghstars - deadlydog/PowerShell.Experiment.ClassPerformanceComparison - A test repo to compare the performance of the different ways to define classes in PowerShell (PowerShell)
README
# PowerShell class performance comparison test
This repo is used to run tests to compare the performance of the different ways to define and use classes in PowerShell:
- PowerShell classes
- Inline C# classes
- Compiled C# classes (import an assembly)You can read more about this experiment and the results [in this blog post](https://blog.danskingdom.com/PowerShell-class-definition-pros-cons-and-performance-comparison/).
## The test
The same classes and enums are defined in each of the three ways, and are imported multiple times into a PowerShell session.
The test measures how long it takes to import the class and enum types into the script.To run the test, run the [Invoke-Test.ps1](/src/Invoke-Test.ps1) script.
### Prerequisites
The test requires dotnet.exe to be installed and available in the PATH, as it is used to build the C# assemblies imported by the test.
## Results
The results of the test on my local machine are:
```text
ImportPowerShellClasses1: 89ms
ImportPowerShellClasses2: 10ms
ImportPowerShellClasses3: 9ms
ImportPowerShellClassesLarge1: 13ms
ImportPowerShellClassesLarge2: 11ms
ImportPowerShellClassesLarge3: 12msImportInlineCSharp1: 764ms
ImportInlineCSharp2: 25ms
ImportInlineCSharp3: 36ms
ImportInlineCSharpLarge1: 230ms
ImportInlineCSharpLarge2: 55ms
ImportInlineCSharpLarge3: 28msImportCSharpAssembly1: 10ms
ImportCSharpAssembly2: 9ms
ImportCSharpAssembly3: 8ms
ImportCSharpAssemblyLarge1: 8ms
ImportCSharpAssemblyLarge2: 8ms
ImportCSharpAssemblyLarge3: 8ms
```Interestingly, the first import of each method is always the slowest, presumably due to required assemblies being loaded into memory the first time they are needed.
We can see that using a C# assembly is the fastest method, followed by PowerShell classes, and then inline C# classes.
The test was also performed by running the import statements in different orders to test if one type of import would impact the performance of another.
The results were always the same, so the order of imports does not seem to matter.